如何将NSString转换为unicode编码转换中文

急急急!!在线求助!swift 怎么把utf-8编码的中文转成gbk编码的呢?_百度知道
急急急!!在线求助!swift 怎么把utf-8编码的中文转成gbk编码的呢?
。我想让他显示中文。百度api里的天气api。返回转成NSString之后;u? 这种格式的如题。怎么转呢,我写歌天气预报,是utf-8编码点?,显示到UITextView上边是&#92?
提问者采纳
用NSJSONSerialization解析一下就行了Etrue):&nbsp.MutableLeaves:var&nbsp,&nil)(json&{\&quot:&&str&sb\&&=&=&;json&nbsp,&nbsp.JSONObjectWithData(str:\&}&\\u3223\\u3233\&NSJSONReadingOptions:&;NSDictionary);var&allowLossyCNSJSONSerialization.dataUsingEncoding(NSUTF8StringEncoding,&nbsp, \uXXXX是 json字符串里面的unicode转义 ;as!&nbsp.objectForKey(&sb&quot你取到的是json字符串吧!
提问者评价
其他类似问题
为您推荐:
贴吧的相关知识
下载知道APP
随时随地咨询
出门在外也不愁如何将NSString转换为Unicode编码_百度知道
如何将NSString转换为Unicode编码
提问者采纳
\ NSString值为Unicode格式的字符串编码(如&#92/u&withString:@&&#92:@&//u开头 + (NSString *)replaceU:(NSString *)unicodeStr { NSString *tempStr1 = [unicodeStrstringByReplacingOccurrencesOfSunicode编码以\u7E8C)转换成中文 /\&#92.;U&quot.
来自团队:
其他类似问题
为您推荐:
下载知道APP
随时随地咨询
出门在外也不愁36815人阅读
iOS开发(98)
iOS中编码转化
1.UTF-8转化
& &&NSString *data = @&你好,北京!&;
& & //转换成UTF-8
& & NSString *dataUTF8 = [data
stringByAddingPercentEscapesUsingEncoding: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 = [dataUTF8
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
& & NSLog(@&%@&,dataGBK);
在Xcode4.2中执行结果如下:
将上述方法封装,如下:
//Unicode转UTF-8
+ (NSString *)encodeToPercentEscapeString: (NSString *) input &
& & // Encode all the reserved characters, per RFC 3986 &
& & // (&&) &
& & NSString *outputStr = (NSString *)& &
& & CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, &
& & & & & & & & & & & & & & & & & & & & & & (CFStringRef)input, &
& & & & & & & & & & & & & & & & & & & & & & NULL, &
& & & & & & & & & & & & & & & & & & & & & & (CFStringRef)@&!*'();:@&=+$,/?%#[]&, &
& & & & & & & & & & & & & & & & & & & & & & kCFStringEncodingUTF8); &
& & return outputS &
+ (NSString *)decodeFromPercentEscapeString: (NSString *) input &
& & NSMutableString *outputStr = [NSMutableString
stringWithString:input]; &
& & [outputStr replaceOccurrencesOfString:@&+& &
&& & & & & & & & & & & & & & & withString:@& & &
& & & & & & & & & & & & & & & & & options:NSLiteralSearch &
& & & & & & & & & & & & & & & & & & range:NSMakeRange(0, [outputStr
length])]; &
& & return [outputStr
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; &
2.UTF-8和Unicode转化
//Unicode转UTF-8
+ (NSString*) replaceUnicode:(NSString*)aUnicodeString
& & NSString *tempStr1 = [aUnicodeString
stringByReplacingOccurrencesOfString:@&\\u&
withString:@&\\U&]; &
& & NSString *tempStr2 = [tempStr1
stringByReplacingOccurrencesOfString:@&\&&
withString:@&\\\&&]; &
& & NSString *tempStr3 = [[@&\&&
stringByAppendingString:tempStr2] stringByAppendingString:@&\&&]; &
& & NSData *tempData = [tempStr3
dataUsingEncoding:NSUTF8StringEncoding]; &
& & NSString* returnStr = [NSPropertyListSerialization
propertyListFromData:tempData &
&& & & & & & & & & & & & & & & & & & & & & & & & & & & & &
mutabilityOption:NSPropertyListImmutable& &
&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
format:NULL &
&& & & & & & & & & & & & & & & & & & & & & & & & & & & & &
errorDescription:NULL]; &
& & return [returnStr
stringByReplacingOccurrencesOfString:@&\\r\\n&
withString:@&\n&];&
+(NSString *) utf8ToUnicode:(NSString *)string
& & NSUInteger length = [string
& & NSMutableString *s = [NSMutableString
stringWithCapacity:0];
& & for (int i =
0;i & i++)&
& & & & unichar _char = [string
characterAtIndex:i];
& & & & //判断是否为英文和数字
& & & & if (_char &= '9' && _char &=
& & & & & & [s appendFormat:@&%@&,[string
substringWithRange:NSMakeRange(i,
& & & & else if(_char &=
'a' && _char &= 'z')
& & & & & & [s appendFormat:@&%@&,[string
substringWithRange:NSMakeRange(i,
&& & & & & &
& & & & else if(_char &=
'A' && _char &= 'Z')
& & & & & & [s appendFormat:@&%@&,[string
substringWithRange:NSMakeRange(i,
&& & & & & &
& & & & else
& & & & & & [s appendFormat:@&\\u%x&,[string
characterAtIndex:i]];
& & return
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:817515次
积分:5521
积分:5521
排名:第3036名
原创:15篇
转载:102篇
评论:76条
(1)(2)(4)(3)(7)(3)(6)(6)(2)(1)(2)(5)(3)(2)(5)(4)(2)(15)(4)(8)(22)(10)<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&

我要回帖

更多关于 unicode编码转换工具 的文章

 

随机推荐