简体   繁体   English

Xcode-UTF-8字符串编码

[英]Xcode - UTF-8 String Encoding

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! 谢谢!

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, @"\ฉ" (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. stringByReplacingPercentEscapesUsingEncoding:表示另一种转义。 See here about percent encoding . 有关百分比编码,请参见此处

What you need to actually do, is parse the string for string representations of unicode code points. 您实际上需要做的是解析该字符串以获取unicode代码点的字符串表示形式。 Here is a link to one potential solution: Using Objective C/Cocoa to unescape unicode characters . 这是一个潜在解决方案的链接: 使用Objective C / Cocoa对unicode字符进行转义 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. 但是,我建议您检查一下正在使用的JSON库(如果使用的是JSON库)-很可能它们提供了某种透明地为您处理此问题的方法。 Eg JSONkit does. 例如, JSONkit可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM