user3378387
user3378387

Reputation: 13

Xcode - UTF-8 String Encoding

I have a strange problem encoding my String

For example:

NSString *str = @"\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13";
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %@", utf);

This worked perfectly in log

utf: ฉันรักคุณ

But, when I try using my string that I parsed from JSON with the same string:

//str is string parse from JSON
NSString *str = [spaces stringByReplacingOccurrencesOfString:@"U" withString:@"u"];
NSLog("str: %@, str);
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %@", utf);

This didn't work in log

str: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
utf: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13

I have been finding the answer for hours but still have no clue

Any would be very much appreciated! Thanks!

Upvotes: 1

Views: 16367

Answers (1)

Janis Kirsteins
Janis Kirsteins

Reputation: 2138

The string returned by JSON is actually different - it contains escaped backslashes (for each "\" you see when printing out the JSON string, what it actually contains is @"\").

In contrast, your manually created string already consists of "ฉันรักคุณ" from the beginning. You do not insert backslash characters - instead, @"\u0e09" (et. al.) is a single code point.

You could replace this line

NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

with this line

NSString *utf = str;

and your example output would not change. The stringByReplacingPercentEscapesUsingEncoding: refers to a different kind of escaping. See here about percent encoding.

What you need to actually do, is parse the string for string representations of unicode code points. Here is a link to one potential solution: Using Objective C/Cocoa to unescape unicode characters. However, I would advise you to check out the JSON library you are using (if you are using one) - it's likely that they provide some way to handle this for you transparently. E.g. JSONkit does.

Upvotes: 2

Related Questions