日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

iOS中編碼轉(zhuǎn)化

系統(tǒng) 3080 0

iOS中編碼轉(zhuǎn)化

?

1.UTF-8轉(zhuǎn)化

?

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

?

? ? // 轉(zhuǎn)換成 UTF-8

?

? ? NSString *dataUTF8 = [data stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];

?

? ? NSLog ( @"%@" ,dataUTF8);

?

? ? //UTF-8 轉(zhuǎn) 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中執(zhí)行結(jié)果如下:

?

?

將上述方法封裝,如下:

?

//Unicode 轉(zhuǎn) 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 = [ NSMutableString stringWithString :input]; ?

?

? ? [outputStr replaceOccurrencesOfString : @"+" ?

?

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? withString : @" " ?

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options : NSLiteralSearch ?

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range : NSMakeRange ( 0 , [outputStr length ])]; ?

?

?? ?

?

? ? return [outputStr stringByReplacingPercentEscapesUsingEncoding : NSUTF8StringEncoding ]; ?

?

}

2.UTF-8和Unicode轉(zhuǎn)化

//Unicode 轉(zhuǎn) 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 length ];

?

? ? NSMutableString *s = [ NSMutableString stringWithCapacity : 0 ];

?

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

?

? ? {

?

? ? ? ? unichar _char = [string characterAtIndex :i];

?

? ? ? ? // 判斷是否為英文和數(shù)字

?

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

?

? ? ? ? {

?

? ? ? ? ? ? [s appendFormat : @"%@" ,[string substringWithRange : NSMakeRange (i, 1 )]];

?

? ? ? ? }

?

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

?

? ? ? ? {

?

? ? ? ? ? ? [s appendFormat : @"%@" ,[string substringWithRange : NSMakeRange (i, 1 )]];

?

?? ? ? ? ? ?

?

? ? ? ? }

?

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

?

? ? ? ? {

?

? ? ? ? ? ? [s appendFormat : @"%@" ,[string substringWithRange : NSMakeRange (i, 1 )]];

?

?? ? ? ? ? ?

?

? ? ? ? }

?

? ? ? ? else

?

? ? ? ? {

?

? ? ? ? ? ? [s appendFormat : @"\\u%x" ,[string characterAtIndex :i]];

?

? ? ? ? }

?

? ? }

?

? ? return s;

?

}

?

iOS中編碼轉(zhuǎn)化


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 措美县| 宁陕县| 伊吾县| 沈阳市| 甘南县| 霍林郭勒市| 昔阳县| 理塘县| 宜兰市| 宽城| 延津县| 鹤庆县| 郎溪县| 曲阜市| 汉沽区| 棋牌| 溧阳市| 卢氏县| 平果县| 武平县| 克东县| 冕宁县| 安远县| 修武县| 翁源县| 九江市| 慈溪市| 新安县| 明水县| 乡宁县| 抚州市| 郸城县| 砚山县| 河北区| 盐山县| 庆安县| 洛隆县| 徐州市| 洪湖市| 土默特左旗| 右玉县|