NSString 中不同类型的 UTF8 解码

发布于 2024-11-26 09:27:11 字数 308 浏览 1 评论 0原文

我搜索了很多关于UTF8解码的信息,但还没有找到答案。

我从我的 NSXMLParser 收到一个 UTF-8 解码 NSString:

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

以某种方式,我找不到将此编码文本更改为:

Test message readability is óké

我可以告诉我尝试过的所有选项,但我认为这没有必要。请帮忙吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

套路撩心 2024-12-03 09:27:11

NSXMLParser 将使用 XML 指定的字符编码来处理文本。我相信在您的情况下,XML 没有明确指定 UTF-8。

该文本似乎是 ISO Latin 1。如果您无法对生成 XML 的服务器执行任何操作,那么您可以应用此 hack:

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

我已经通过从 GDB 提示符测试它来验证此方法是否有效:

po [NSString stringWithUTF8String:(char*)[@"Test message readability is óké" cStringUsingEncoding:5]]

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]]
来日方长 2024-12-03 09:27:11

你做错了。您想要的是:

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];

还请记住,当您初始化字符串常量时,实际进入程序内存的内容取决于当前文件的编码。如果它已经是 UTF-8,那么字符将被双重编码 - 您将在 C 字符串中得到编码为 UTF8 的字符 à、³ 等。

换句话说,使用字符串常量可能从一开始就是一个错误的举动。请提供问题的更多背景信息。

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.

盛夏尉蓝 2024-12-03 09:27:11

标准编码和解码如下:

对于编码:

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

对于解码:

NSString *decodedString = [msg.content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Standart encoding and decoding like this:

For encoding:

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

For decoding:

NSString *decodedString = [msg.content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文