joels
joels

Reputation: 7749

unicode escapes in objective-c

I have a string "Artîsté". I use json_encode from PHP on it and I get "Art\u00eest\u00e9".

How do I convert that to an NSString? I have tried many things and none of them work I always end up getting Artîsté

For Example:
NSString stringWithUTF8String:"Art\u00c3\u00aest\u00c3\u00a9"];//Artîsté
@"Art\u00c3\u00aest\u00c3\u00a9"; //Artîsté

Upvotes: 5

Views: 4682

Answers (2)

bames53
bames53

Reputation: 88235

The problem is your input string:

"Art\u00c3\u00aest\u00c3\u00a9"

does in fact literally mean "Artîsté". \u00c3 is 'Ã', \u00ae is '®', and \u00a9 is '©'.

Whatever is producing your input string is receiving UTF-8 input but expecting something else (e.g., cp1252, ISO-8859-1, or ISO-8859-15)

Upvotes: 4

rob mayoff
rob mayoff

Reputation: 386058

You can use CFStringCreateFromExternalRepresentation with the kCFStringEncodingNonLossyASCII encoding to parse the \uXXXX escape sequences. Check out my answer here:

Converting escaped UTF8 characters back to their original form

Upvotes: 7

Related Questions