码迷,mamicode.com
首页 > 其他好文 > 详细

将UTF8编码转化为中文 - NSString方法

时间:2015-01-10 12:36:25      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

方法一:

代码如下,如有更好的方法 麻烦贴出来,这个方法是通过webview进行解码的
        UIWebView *web = [[UIWebView alloc] init];
    NSString *tsw = @"%E4%B8%AD%E5%9B%BD";
    NSString *sc = [NSString stringWithFormat:@"decodeURIComponent(‘%@‘)",tsw];
    NSString *st = [web stringByEvaluatingJavaScriptFromString:sc];
    NSLog(st);
    [web release]; 

方法二:

测试了一下,搞定了,用NSString的stringByReplacingPercentEscapesUsingEncoding方法就可以了,可以这样子:

    NSString* strAfterDecodeByUTF8AndURI = [@"%E4%B8%AD%E5%9B%BD" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"strAfterDecodeByUTF8AndURI=%@", strAfterDecodeByUTF8AndURI);    

这个问题的本质时,首先这段内容是utf-8编码,然后又进行了URL Encode,所以解码的时候,先URL Decode,再utf-8解码即可

什么是url encode参见 http://www.stringfunction.com/url-decode.html

所以

stringByReplacingPercentEscapesUsingEncoding 方法是用于url decode

然后其中的参数NSUTF8StringEncoding是指定了UTF-8编码即可

=================================

关于 http://www.cocoachina.com/bbs/read.php?tid-16787.html 的实现,也做了些分析如下:

从原理上解释下这种做法。

编码定义,见下面的c)

A: There are three or four options for making Unicode fit into an 8-bit format.

a) Use UTF-8. This preserves ASCII, but not Latin-1, because the characters >127 are different from Latin-1. UTF-8 uses the bytes in the ASCII only for ASCII characters. Therefore, it works well in any environment where ASCII characters have a significance as syntax characters, e.g. file name syntaxes, markup languages, etc., but where the all other characters may use arbitrary bytes.
Example: “Latin Small Letter s with Acute” (015B) would be encoded as two bytes: C5 9B.

b) Use Java or C style escapes, of the form \uXXXXX or \xXXXXX. This format is not standard for text files, but well defined in the framework of the languages in question, primarily for source files.
Example: The Polish word “wyj?cie” with character “Latin Small Letter s with Acute” (015B) in the middle (? is one character) would look like: “wyj\u015Bcie".

c) Use the &#xXXXX; or &#DDDDD; numeric character escapes as in HTML or XML. Again, these are not standard for plain text files, but well defined within the framework of these markup languages.
Example: “wyj?cie” would look like “wyjście"

d) Use SCSU. This format compresses Unicode into 8-bit format, preserving most of ASCII, but using some of the control codes as commands for the decoder. However, while ASCII text will look like ASCII text after being encoded in SCSU, other characters may occasionally be encoded with the same byte values, making SCSU unsuitable for 8-bit channels that blindly interpret any of the bytes as ASCII characters.
Example: “<SC2> wyjÛcie” where <SC2> indicates the byte 0x12 and “Û” corresponds to byte 0xDB.

如c所描述,这是一种“未标准"但广泛采用的做法,说是山寨编码也行 :-)

所以编码过程是

字符串 -> Unicode编码 -> &#xXXXX; or &#DDDDD; 

解码过程反过来即可

注意:由于这种编码方式是“山寨”未“标准”的编码,所以iPhone的SDK没有支持(无法向上面utf-8编码一样),只能自己搞定(也不是很难了,见dboylx的实现)

将UTF8编码转化为中文 - NSString方法

标签:

原文地址:http://www.cnblogs.com/pandas/p/4214642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!