NSString 中不同类型的 UTF8 解码
我搜索了很多关于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSXMLParser 将使用 XML 指定的字符编码来处理文本。我相信在您的情况下,XML 没有明确指定 UTF-8。
该文本似乎是 ISO Latin 1。如果您无法对生成 XML 的服务器执行任何操作,那么您可以应用此 hack:
我已经通过从 GDB 提示符测试它来验证此方法是否有效:
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:
I have verified that this works by testing this from the GDB prompt:
你做错了。您想要的是:
还请记住,当您初始化字符串常量时,实际进入程序内存的内容取决于当前文件的编码。如果它已经是 UTF-8,那么字符将被双重编码 - 您将在 C 字符串中得到编码为 UTF8 的字符 à、³ 等。
换句话说,使用字符串常量可能从一开始就是一个错误的举动。请提供问题的更多背景信息。
You're doing it wrong. What you want is:
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.
标准编码和解码如下:
对于编码:
对于解码:
Standart encoding and decoding like this:
For encoding:
For decoding: