原文地址:http://blog.csdn.net/huifeidexin_1/article/details/7883984

iOS中编码转化

1.UTF-8转化

NSString *data = @"你好,北京!";

//转换成UTF-8

NSString *dataUTF8 = [datastringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@",dataUTF8);

//UTF-8转GBK,将UTF8代码替换,官方解释如下。

//Replaces all percent escapes with the matching characters as determined by the given encoding.  Returns nil if the transformation is not possible (i.e. the percent escapes give a byte sequence not legal in the given encoding).  See CFURLCreateStringByReplacingPercentEscapes in CFURL.h for more complex transformations

NSString *dataGBK = [dataUTF8stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@",dataGBK);

在Xcode4.2中执行结果如下:

将上述方法封装,如下:

//Unicode转UTF-8

+ (NSString *)encodeToPercentEscapeString: (NSString *) input

{

// Encode all the reserved characters, per RFC 3986

// (<http://www.ietf.org/rfc/rfc3986.txt>)

NSString *outputStr = (NSString *)

CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,

(CFStringRef)input,

NULL,

(CFStringRef)@"!*'();:@&=+$,/?%#[]",

kCFStringEncodingUTF8);

return outputStr;

}

+ (NSString *)decodeFromPercentEscapeString: (NSString *) input

{

NSMutableString *outputStr = [NSMutableStringstringWithString:input];

[outputStr replaceOccurrencesOfString:@"+"

withString:@" "

options:NSLiteralSearch

range:NSMakeRange(0, [outputStrlength])];

return [outputStrstringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

}

2.UTF-8和Unicode转化

//Unicode转UTF-8

+ (NSString*) replaceUnicode:(NSString*)aUnicodeString

{

NSString *tempStr1 = [aUnicodeStringstringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];

NSString *tempStr2 = [tempStr1stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];

NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];

NSData *tempData = [tempStr3dataUsingEncoding:NSUTF8StringEncoding];

NSString* returnStr = [NSPropertyListSerializationpropertyListFromData:tempData

mutabilityOption:NSPropertyListImmutable

format:NULL

errorDescription:NULL];

return [returnStrstringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];

}

+(NSString *) utf8ToUnicode:(NSString *)string

{

NSUInteger length = [string length];

NSMutableString *s = [NSMutableStringstringWithCapacity:0];

for (int i = 0;i < length; i++)

{

unichar _char = [string characterAtIndex:i];

//判断是否为英文和数字

if (_char <= '9' && _char >='0')

{

[s appendFormat:@"%@",[stringsubstringWithRange:NSMakeRange(i,1)]];

}

else if(_char >='a' && _char <= 'z')

{

[s appendFormat:@"%@",[stringsubstringWithRange:NSMakeRange(i,1)]];

}

else if(_char >='A' && _char <= 'Z')

{

[s appendFormat:@"%@",[stringsubstringWithRange:NSMakeRange(i,1)]];

}

else

{

[s appendFormat:@"\\u%x",[stringcharacterAtIndex:i]];

}

}

return s;

}

IOS编码转化的更多相关文章

  1. iOS编码规范参考

    目录      注释 1.1  多行注释 1.2  单行注释 1.3  函数的注释   命名 2.1  常量的命名 2.2  函数的命名 2.3  变量的命名 2.3.1  成员变量 2.3.2  公 ...

  2. golang 编码转化

    在网上搜索golang编码转化时,我们经常看到的文章是使用下面一些第三方库: https://github.com/djimenez/iconv-go https://github.com/qiniu ...

  3. Nodejs编码转化问题

    目前Node.js仅支持hex.utf8.ascii.binary.base64.ucs2几种编码的转换.对于GBK,GB2312等编码,Nodejs自带的toString()方法不支持,因此中文转化 ...

  4. android iOS 编码问题害死人!

    android 与后端服务器进行通信时,默认使用的编码格式是asi. 而iOS与后端通信时,获取的数据到iOS端默认被utf-8进行编码.所以,我们常常出现android能够从服务器端获取到数据,但是 ...

  5. 宽字符、多字节、unicode、utf-8、gbk编码转化

    今天遇到一个编码的问题,困惑了我很长时间,所以就简要的的了解了一下常用的编码类型. 我们最常见的是assic编码,它是一种单字节编码,对多容纳256个字符. 我们在编程的时候经常遇到unicode,u ...

  6. iOS编码规范

      The official raywenderlich.com Objective-C style guide.   This style guide outlines the coding con ...

  7. [ACM_模拟] POJ1068 Parencodings (两种括号编码转化 规律 模拟)

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

  8. 报表开发工具中mysql数据库连接编码转化失效解决方案

    1. 问题描述 在报表开发工具FineReport中,mysql数据库连接通过数据连接编码转换进行编码的转换,在通过报表录入往数据库中录入中文数据的时候,总是出现乱码,这个该怎么解决呢? 2. 解决方 ...

  9. ios编码转换 国标 UTF-8

    我们知道,使用NSURLConnection的代理方法下载网页,存到一个NSData中, NSMutableData *pageData; [pageData appendData:data]; 如果 ...

随机推荐

  1. shiro的简单入门使用

    这里只是测试登录认证,没有web模块,没有连接数据库,用户密码放在shiro.ini配置中,密码没有加密处理,简单入门. 基于maven 先看目录结构 测试结果 pom.xml <?xml ve ...

  2. Github客户端操作

    Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.作为一个程序员,我们需要掌握其用法. 作为开源代码库以及版本控制系统,Github目前拥有140 ...

  3. 字符集(编码)转换_Qt532_QString

    1.网上的资料: 1.1.参考网址:http://blog.csdn.net/changsheng230/article/details/6588447 1.2.网页内容: “ Qt 使用Unicod ...

  4. [原][osg][osgearth]简单的通过osgDB,读取高程tif,修改高程tif

    ReadResult result; osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()-> ...

  5. 附加题找bug

    private: void ReSize(int sz) { ) { return; } if(maxSize != sz) { T *arr = new T[sz]; if(arr == NULL) ...

  6. 【Golang】解决Go test执行单个测试文件提示未定义问题

    背景 很多人记录过怎么执行Go test单个文件或者单个函数,但是要么对执行单文件用例存在函数或变量引用的场景避而不谈,要么提示调用了其它文件中的模块会报错.其实了解了go test命令的机制之后,这 ...

  7. Assert.IsNotNull 方法(判断对象不为NULL)

    Assert.IsNotNull 方法 Visual Studio 2012   其他版本 Visual Studio 2010 Visual Studio 2008 Visual Studio 20 ...

  8. 12月12日 has_many through:的interference, option

    has_many :products, through: :cart_items, source: :product build定义:collection.build(attributes = {}, ...

  9. TitanX服务器登陆网关

  10. Deep Learning of Graph Matching 阅读笔记

    Deep Learning of Graph Matching 阅读笔记 CVPR2018的一篇文章,主要提出了一种利用深度神经网络实现端到端图匹配(Graph Matching)的方法. 该篇文章理 ...