本文主要是介绍Objective-c NSString 转utf-8和gb2312,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转载自:http://www.cocoachina.com/bbs/read.php?tid=62897
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #pragma mark - #pragma mark Encode Chinese to ISO8859-1 in URL -( NSString *)EncodeUTF8Str:( NSString *)encodeStr{ CFStringRef nonAlphaNumValidChars = CFSTR( "![ DISCUZ_CODE_1 ]’()*+,-./:;=?@_~" ); NSString *preprocessedString = ( NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)encodeStr, CFSTR( "" ), kCFStringEncodingUTF8); NSString *newStr = [( NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)preprocessedString, NULL ,nonAlphaNumValidChars,kCFStringEncodingUTF8) autorelease]; [preprocessedString release]; return newStr; } #pragma mark - #pragma mark Encode Chinese to GB2312 in URL -( NSString *)EncodeGB2312Str:( NSString *)encodeStr{ CFStringRef nonAlphaNumValidChars = CFSTR( "![ DISCUZ_CODE_1 ]’()*+,-./:;=?@_~" ); NSString *preprocessedString = ( NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)encodeStr, CFSTR( "" ), kCFStringEncodingGB_18030_2000); NSString *newStr = [( NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)preprocessedString, NULL ,nonAlphaNumValidChars,kCFStringEncodingGB_18030_2000) autorelease]; [preprocessedString release]; return newStr; } | |
使用sudzc连接webService时出现了中文乱码问题,使用NSUTF8encoding 依然是乱码。
使用上面的EncodeUTF8Str方法转码,比如:苹果,转码后是 %E8%8B%B9%E6%9E%9C,这样服务器端解码就可以了。
这篇关于Objective-c NSString 转utf-8和gb2312的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!