Xcode - UTF-8 字符串编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22168540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode - UTF-8 String Encoding
提问by user3378387
I have a strange problem encoding my String
我有一个奇怪的问题编码我的 String
For example:
例如:
NSString *str = @"\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13";
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %@", utf);
This worked perfectly in log
这在日志中完美运行
utf: ?????????
But, when I try using my string that I parsed from JSON with the same string:
但是,当我尝试使用我从 JSON 解析的字符串时,使用相同的字符串:
//str is string parse from JSON
NSString *str = [spaces stringByReplacingOccurrencesOfString:@"U" withString:@"u"];
NSLog("str: %@, str);
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %@", utf);
This didn't work in log
这在日志中不起作用
str: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
utf: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
I have been finding the answer for hours but still have no clue
我已经找了几个小时的答案,但仍然不知道
Any would be very much appreciated! Thanks!
任何将不胜感激!谢谢!
回答by Janis Kirsteins
The string returned by JSON is actually different - it contains escaped backslashes (for each "\" you see when printing out the JSON string, what it actually contains is @"\").
JSON 返回的字符串实际上是不同的 - 它包含转义的反斜杠(对于您在打印出 JSON 字符串时看到的每个“\”,它实际包含的是@“\”)。
In contrast, your manually created string already consists of "?????????" from the beginning. You do not insert backslash characters - instead, @"\u0e09" (et. al.) is a single code point.
相比之下,您手动创建的字符串已经包含“?????????” 从一开始就。您不插入反斜杠字符 - 相反,@"\u0e09"(等)是单个代码点。
You could replace this line
你可以替换这条线
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
with this line
用这条线
NSString *utf = str;
and your example output would not change. The stringByReplacingPercentEscapesUsingEncoding: refers to a different kind of escaping. See here about percent encoding.
并且您的示例输出不会改变。stringByReplacingPercentEscapesUsingEncoding: 指的是另一种转义。请参阅此处了解百分比编码。
What you need to actually do, is parse the string for string representations of unicode code points. Here is a link to one potential solution: Using Objective C/Cocoa to unescape unicode characters. However, I would advise you to check out the JSON library you are using (if you are using one) - it's likely that they provide some way to handle this for you transparently. E.g. JSONkitdoes.
您实际需要做的是解析字符串以获取 unicode 代码点的字符串表示形式。这是一个潜在解决方案的链接:Using Objective C/Cocoa to unescape unicode characters。但是,我建议您查看您正在使用的 JSON 库(如果您正在使用)——它们很可能提供了一些方法来透明地为您处理这个问题。例如JSONkit可以。