Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

iphone - UTF8 character decoding in Objective C

I am trying to parse a feed from a json webservice on my iPhone however the utf8 conversion is not working the way it should or am I doing something wrong Here is apart of the feed
est un ru00c3u00aave en noir

Here is the code I have written to convert the above:

NSString* str1 = @"est un r u00c3u00aa ve en noir";

NSString* str = [NSString stringWithUTF8String:[str1 cStringUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Converted UTF   %@",str);

This is the output that I am getting:
Converted UTF est un r ?a ve en noir

The expected output should be
Converted UTF est un rêve en noir

When I checked the UTF table and used a utf converter online the output is correctly got with the same code and using the string @"est un ru00EAve en noir" but I think that the UTF16 Representation if I am not mistaken. Now am really in a fix how to parse this feed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After much grief I have figured this out. Use this.

NSString *correctString = [NSString stringWithCString:[utf8String cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...