方法一:

代码如下,如有更好的方法 麻烦贴出来,这个方法是通过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方法的更多相关文章

  1. Linux Centos7设置UTF-8编码,防止中文乱码

    Linux Centos7设置UTF-8编码,防止中文乱码 # localeLANG=zh_CN.gb2312LC_CTYPE="zh_CN.gb2312"LC_NUMERIC=& ...

  2. php计算字符串长度:utf8编码,包含中文

    php计算字符串长度:utf8编码 中文当作1个字符处理(strlen默认当作两个字符) 上函数: /** * 计算 UTF-8 字符串长度 * * @param string $str * @ret ...

  3. Latex中文utf-8编码的三种方式

    我们知道Latex一般用CJK和CTEX宏包支持中文编辑,CJK和CTEX的默认编码是GBK,而windows下的默然编码就是GBK,因此CJK和CTEX不需要特殊配置就可以直接支持中文Latex编译 ...

  4. Python MySQLdb 使用utf-8 编码插入中文数据

    参考地址:http://blog.csdn.net/dkman803/article/details/1925326/ 本人在使用python,mysqldb操作数据库的时候,发现如下问题,编码如下: ...

  5. 解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG

    解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-6 ...

  6. 服务器返回中文乱码的情况(UTF8编码 -> 转化为 SYSTEM_LOCALE 编码)

    服务器乱码 转换使用如下方法 入惨{“msg”} -> utf8编码 -> 转化为 SYSTEM_LOCALE 编码 -> 接受转换后的参数 "sEncoding" ...

  7. eclipse 解决编译出现GBK或UTF8 编码错误的方法

    eclipse由于开源所以支持了比较杂的编码方式,而这些一个工程导入时添加了不少的外来程序,由于不是同一工程一次编码带来了其中含有GBK和 UTF8   UTF16  ASCII等文件编译时就会出现错 ...

  8. JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码

    JSP,mysql,tomcat下(基于struts2)中文及其乱码问题的解决 6大配置点 使用UTF-8编码 目前对遇到J2EE 开发中 中文及其乱码问题,参考网上资料做个总结, 主要是6大配置点: ...

  9. python利用utf-8编码判断中文英文字符(转)

    下面这个小工具包含了判断unicode是否是汉字.数字.英文或者其他字符,全角符号转半角符号,unicode字符串归一化等工作. #!/usr/bin/env python # -*- coding: ...

随机推荐

  1. Template - Strategy

    模板模式是一种基于继承的松耦合模式,其设计思路为,abstract类提供一组接口但不实现,不同concrete类继承同一接口并完成不同功能.如下图所示: 模板模式实现较为简单,TemplateMeth ...

  2. 用for、while、do-while循环输出10句“好好学习,天天向上!”

    #include "stdio.h" void main() { int time; ;time<=;time++) printf("%d.好好学习,天天向上!\n ...

  3. [转]Linux挂载点介绍及桌面服务器分区方案

    原链接:http://www.metsky.com/archives/255.html 本文介绍Linux常用分区挂载点常识以及桌面.服务器分区挂载点的推荐配置,当然这个配置是天缘自己写的,分区大小这 ...

  4. 常用的JS页面跳转代码调用大全

    一.常规的JS页面跳转代码 1.在原来的窗体中直接跳转用 <script type="text/javascript"> window.location.href=&q ...

  5. UVALive 2521 Game Prediction 题解

    这个我上来把题目理解错了,我以为所有人的牌都是一样的,感觉这个题太麻烦了吧,而且题目样例过不去啊……后来发现理解错了,给出的数据是他一个人的数据,就是让我们求他一定能赢的轮数,所有的牌是固定的(1 - ...

  6. PowerDesigner中逆向工程将数据库中comment赋值到name

    '------------------------------------------------------------ ' '脚本功能: ' PowerDesigner中逆向工程完成后,将数据库中 ...

  7. jq中的ajax

    jq对ajax进行了封装,在jq中$.ajax()方法是最底层的方法,第二层是load() , get() , post()方法,第三层是$.getScript()和$.getJSON().基本第二种 ...

  8. 剑指offer 链表中倒数第K个节点

    利用两个间隔为k的指针来实现倒数第k个 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...

  9. Redis 密码设置和登录

    Redis 一般在生产环境中,大家都不使用密码,为了确保安全,都是在防火墙上对redis端口做IP白名单的 我是个技术控,我非得了解一下密码这回事[虽然以后不会用到,呵呵] 好了,废话不多说,简单介绍 ...

  10. jquery判断节点是否存在

    if($('.onloadMore').length>0){ return '节点存在'; }else{ return '节点不存在'; }