swift - Unprintable ASCII character found in source file - encode

This code:
var str = "ぴ" //Japanese: pi
xcode report compilation error:
Unprintable ASCII character found in source file

Have you tried this?
func convertString(string: String) -> String {
var data = string.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
return NSString(data: data!, encoding: NSASCIIStringEncoding) as! String
}
From How to deal with a user input string that gives an "unprintable ascii character found in source file" error when pasted into Xcode?

This answer is for the people who are using windows keyboard with mac system.
While typing you might have unknowingly clicked the right-click key in your keyboard, which will create the Unprintable ASCII character that is causing the Unprintable ASCII character found in the source file error.
To check this go to find(cmd+F) and click the right-click key and you can find them in your class and delete those characters and the error will be gone.
Thanks

SWIFT 4:
func convertString(string: String) -> String {
let data = string.data(using: String.Encoding.ascii, allowLossyConversion: true)
return NSString(data: data!, encoding: String.Encoding.ascii.rawValue)! as String
}

In a Playground your line won't give you an error as it is, since it uses UTF-8 encoding by default. In a regular project your trusty File inspector will allow you to define the Text Encoding in the Text Settings section. I recommend to use some version of Unicode, probably UTF-8 if your app will deal primarily with western languages and UTF-16 if you have a lot of text constants in asian languages.
This just for the record since this is an old question and you probably found a working solution years ago.

Related

swift read xls(excel) content + Hindi + Gujarati Character Encoding issue

read xls(Excel) file with non english character(**not xlsx file)
i have tried all the encoding option but i am unable to read xls file content.
let data = try Data(contentsOf: url)// Document Directory file path
let dataEncoded = String(data: data, encoding: .utf8)
Example
Thanks in advance.
resolved my issue through below SDK:
https://github.com/QuetzalMX/QuetzalXLSReader

ios sdk URL filename, remove the url addon special character "\"

In iOS sdk (swift). Lets say i have a three files "mary'sCat.mp3", "mary\s.mp3", "mary\\s.mp3"
(the special character \ is part of the real filename)
When I use the below code to get the urls
FileManager.default.contentsOfDirectory(at: documentDir, includingPropertiesForKeys: nil)
and use the below code to get filename
fileUrl.standardizedFileURL.lastPathComponent
I will have "mary\'sCat.mp3", "mary\\s.mp3", "mary\\\\s.mp3"
So.. is there any way i can remove the system addon special characters \ in a correct way? so i can get back to original file name "mary'sCat.mp3", "mary\s.mp3", "mary\\s.mp3"? I noticed that when using XCode output window you wont see the addon special character, but when you actually see it in debug watch, u will see the addon special character. That is nightmare when doing String compare as below
let mediaUrl_filename = "mary\\s.mp3" \\<-- this value from url
let db_filename = "mary\s.mp3" \\ <-- this value from sqlite
if mediaUrl_filename == db_filename {
print("It is equal")
}
So is there any way to solve this problem?
Actually '\' is used as escape sequence in String objects so while comparing the file name if the file name is 'mary\s.mp3' you will have to write like this
if mediaUrl_filename == "mary\\s.mp3"{
print("It is equal")
}
I suggest that you replace the '\' with other character like '_' in the file name to ignore such confusions.

How to read GB2312 encoded text files using Swift 3.0

My app needs to read text files encoded in GB2312. Here is the current code.
SGFString = try String(contentsOf:path)
It throws an exception.
couldn’t be opened because the text encoding of its contents can’t be
determined.
While looking into the String.Encoding values, there is no GB2312. What would be the best way to read the GB2312 text files?
Thanks
Ray
Thanks for OOPer's help. I copied some of the code from the links, now it works.
Here is the code:
extension String.Encoding {
static let gb_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
}
Gb2312String = try String(contentsOf:path, encoding:String.Encoding.gb_18030_2000)

How to handle iOS string characters parsing in node (Japanese characters)?

I'm running into issues uploading an ios strings file (english -> japanese) to a node server for parsing.
The file is UTF-16LE but when parsed as a string the character encoding loses characters. This may have something to do with express using utf8 to read in the request file data which malforms the file data.
When the file is loaded in atom/sublime w/ utf16 encoding it works great
When the file is loaded in utf8 things break down.
Any help would be awesome.
After doing some research and digging.
Utilizing npm module iconv-lite to parse the file buffer one should:
1) parse the buffer as utf16le
2) down convert to utf8
3) conver to a string.
if (encoding === 'utf-16le') {
str = iconv.decode(buffer, 'utf16le');
body = iconv.encode(str, 'utf8').toString();
} else if (encoding === 'utf-16be') {
str = iconv.decode(buffer, 'utf16be');
body = iconv.encode(str, 'utf8').toString();
} else {
body = Buffer.concat(file.data).toString();
}

BlackBerry - language support for Chinese

I have localised my app by adding the correct resource files for various European languages / dialects.
I have the required folder in my project: ./res/com/demo/localization
It contains the required files e.g. Demo.rrh, Demo.rrc, Demo_de.rrc etc.
I want to add support for 2 Chinese dialects, and I have the translations in an Excel file. On iPhone, they are referred to by the codes zh_TW & zh_CM. Following the pattern with German, I created 2 extra files called Demo_zh_TW.rrc & Demo_zh_CN.rrc.
I opened file Demo_zh_CN.rrc using Eclipse's text editor, and pasted in line of the Chinese translation using the normal resource file format:
START_LOCATION#0="开始位置";
When I tried to save the file, I got Eclipse's error about the Cp1252 character encoding:
Save could not be completed.
Reason:
Some characters cannot be mapped using "Cp1252" character encoding.
Either change the encoding or remove the characters which are not
supported by the "Cp1252" character encoding.
It seems the Eclipse editor will accept the Chinese characters, but the resource tool expects that these characters must be saved in the resource file as Java Unicode /u encoding.
How do I add language support for these 2 regions without manually copy n pasting in each string?
Is there maybe a tool that I can use to Java Unicode /u encode the strings from Excel so they can be saved in Code page 1252 Latin chars only?
I'm not aware of any readily available tools for working with BlackBerry's peculiar localization style.
Here's a snippet of Java-SE code I use to convert the UTF-8 strings I get for use with BlackBerry:
private static String unicodeEscape(String value, CharsetEncoder encoder) {
StringBuilder sb = new StringBuilder();
for(char c : value.toCharArray()) {
if(encoder.canEncode(c)) {
sb.append(c);
} else {
sb.append("\\u");
sb.append(hex4(c));
}
}
return sb.toString();
}
private static String hex4(char c) {
String ret = Integer.toHexString(c);
while(ret.length() < 4) {
ret = "0" + ret;
}
return ret;
}
Call unicodeEscape with the 8859-1 encoder with Charset.forName("ISO-8859-1").newEncoder()
I suggest you look at Blackberry Hindi and Gujarati text display
You need to use the resource editor to make these files with the right encoding. Eclipse will escape the characters automatically.
This is a problem with the encoding of your resource file. 1252 Code Page contains Latin characters only.
I have never worked with Eclipse, but there should be somewhere you specify the encoding of the file, you should set your default encoding for files to UTF-8 if possible. This will handle your chinese characters.
You could also use a good editor like Notepad++ or EMEditor to set the encoding of your file.
See here for how you can configure Eclipse to use UTF-8 by default.

Resources