Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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é
like image 625
joels Avatar asked Oct 25 '11 18:10

joels


2 Answers

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

like image 95
rob mayoff Avatar answered Sep 30 '22 19:09

rob mayoff


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)

like image 32
bames53 Avatar answered Sep 30 '22 20:09

bames53