Different kind of UTF8 decoding in NSString

2019-05-23 16:45发布

I have searched a lot about UTF8 decoding, but not found the answer yet.

I receive an UTF-8 decode NSString from my NSXMLParser:

NSString *tempString = @"Test message readability is óké";

In someway I can't find the way to change this encoded text to:

Test message readability is óké

I could tell all the options I tried but I don't think that should be necessary. Could please some help?

Thnx!

3条回答
在下西门庆
2楼-- · 2019-05-23 17:01

You're doing it wrong. What you want is:

char *s = "Test message readability is óké";
//Note: this is a one-byte-character C string, not an NSString!
NSString *tempString = [NSString stringWithCString:s encoding:NSUTF8StringEncoding];

Also keep in mind that when you initialize string constants, what actually goes to program memory depends on the encoding of the current file. If it's already UTF-8, then the characters will be doubly-encoded - you'll get characters Ã,³, etc. encoded as UTF8 in the C string.

In other words, using a string constant is probably a wrong move to begin with. Please give more context to the problem.

查看更多
Rolldiameter
3楼-- · 2019-05-23 17:02

The NSXMLParser will treat the text using the character encoding that the XML specifies. I believe in your case the XML do not specify UTF-8 explicitly.

The text seems to be ISO Latin 1. If you can not do anything about the server generating the XML then you can apply this hack:

char* tempString = [string cStringUsingEncoding:NSISOLatin1StringEncoding];
string = [NSString stringWithUTF8String:tempString];

I have verified that this works by testing this from the GDB prompt:

po [NSString stringWithUTF8String:(char*)[@"Test message readability is óké" cStringUsingEncoding:5]]
查看更多
Summer. ? 凉城
4楼-- · 2019-05-23 17:20

Standart encoding and decoding like this:

For encoding:

NSString *content =  [bodyTextView.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

For decoding:

NSString *decodedString = [msg.content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
查看更多
登录 后发表回答