将UTF8编码转化为中文 - NSString方法
方法一:
代码如下,如有更好的方法 麻烦贴出来,这个方法是通过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方法的更多相关文章
- Linux Centos7设置UTF-8编码,防止中文乱码
Linux Centos7设置UTF-8编码,防止中文乱码 # localeLANG=zh_CN.gb2312LC_CTYPE="zh_CN.gb2312"LC_NUMERIC=& ...
- php计算字符串长度:utf8编码,包含中文
php计算字符串长度:utf8编码 中文当作1个字符处理(strlen默认当作两个字符) 上函数: /** * 计算 UTF-8 字符串长度 * * @param string $str * @ret ...
- Latex中文utf-8编码的三种方式
我们知道Latex一般用CJK和CTEX宏包支持中文编辑,CJK和CTEX的默认编码是GBK,而windows下的默然编码就是GBK,因此CJK和CTEX不需要特殊配置就可以直接支持中文Latex编译 ...
- Python MySQLdb 使用utf-8 编码插入中文数据
参考地址:http://blog.csdn.net/dkman803/article/details/1925326/ 本人在使用python,mysqldb操作数据库的时候,发现如下问题,编码如下: ...
- 解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG
解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-6 ...
- 服务器返回中文乱码的情况(UTF8编码 -> 转化为 SYSTEM_LOCALE 编码)
服务器乱码 转换使用如下方法 入惨{“msg”} -> utf8编码 -> 转化为 SYSTEM_LOCALE 编码 -> 接受转换后的参数 "sEncoding" ...
- eclipse 解决编译出现GBK或UTF8 编码错误的方法
eclipse由于开源所以支持了比较杂的编码方式,而这些一个工程导入时添加了不少的外来程序,由于不是同一工程一次编码带来了其中含有GBK和 UTF8 UTF16 ASCII等文件编译时就会出现错 ...
- JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码
JSP,mysql,tomcat下(基于struts2)中文及其乱码问题的解决 6大配置点 使用UTF-8编码 目前对遇到J2EE 开发中 中文及其乱码问题,参考网上资料做个总结, 主要是6大配置点: ...
- python利用utf-8编码判断中文英文字符(转)
下面这个小工具包含了判断unicode是否是汉字.数字.英文或者其他字符,全角符号转半角符号,unicode字符串归一化等工作. #!/usr/bin/env python # -*- coding: ...
随机推荐
- properties 配置文件如何换行
在使用properties配置文件的时候我们经常碰到如下两个问题 1:当a=b中的b值内容特别长的时候为了阅读方便我们手动换行,但如果我们直接回车那么后面的数据就会丢失.那如何解决呢? 例如: a=a ...
- 【转】VC6.0打开或者添加工程文件崩溃的解决方法
很多学习编程的同学都遇到这样的问题,在Windows操作系统下使用Visual C++ 6.0编程时,如果点击菜单中的[打开]或者[添加],或者按快捷键,都会弹出下图的对话框,出现程序崩溃并退出的情况 ...
- POJ 1308/并查集
题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2 ...
- web配置文件的<load-on-startup>0</load-on-startup>
在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是:标记容器是否在启动的时候就加载这个servlet.当值为0或者大于 ...
- python 基本的序列和映射规则
>>> def checkIndex(key):... if not isinstance(key,(int,long)):raise TypeError... if ...
- 动画——animation(2)
日常中,我们使用的动画来源有两个方面—— 第一个,自己去定义. 通过@keyframes去定义即可,格式如下: @keyframe animatename{ 0%{ //这里面写初始的对象的css样式 ...
- hdu Eddy's picture (最小生成树)
Eddy's picture Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- Android平台设计规范整理(尺寸+组成元素+字体+滑块)
转自:http://www.ui.cn/project.php?id=12394
- mysql笔记4之数据操作
1修改数据 插入:insert into stu(id,name,age,addr) values(2,"李四",44,"重庆"); 2修改某一列 updata ...
- SEO优化之 主页上加上nofollow
<a href=http://www.主页.cn/ rel=”nofollow”>这里是锚文字</a> <光年日志分析系统>来分析抓取比较多的是哪个网页,没用的no ...