首页 > 代码库 > ios基本编码格式转换UTF8

ios基本编码格式转换UTF8

iOS中编码转化

1.UTF-8转化

 

    NSString *data =http://www.mamicode.com/ @"你好,北京!";

    //转换成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中执行结果如下:

技术分享

将上述方法封装,如下:

 

 1 //Unicode转UTF-8 2  3   4  5 + (NSString *)encodeToPercentEscapeString: (NSString *) input   6  7 {   8  9     // Encode all the reserved characters, per RFC 3986  10 11     // (<http://www.ietf.org/rfc/rfc3986.txt>)  12 13     NSString *outputStr = (NSString *)   14 15     CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  16 17                                             (CFStringRef)input,  18 19                                             NULL,  20 21                                             (CFStringRef)@"!*‘();:@&=+$,/?%#[]",  22 23                                             kCFStringEncodingUTF8);  24 25     return outputStr;  26 27 }  28 29  30 31 + (NSString *)decodeFromPercentEscapeString: (NSString *) input  32 33 {  34 35     NSMutableString *outputStr = [NSMutableString stringWithString:input];  36 37     [outputStr replaceOccurrencesOfString:@"+"  38 39                                withString:@" "  40 41                                   options:NSLiteralSearch  42 43                                     range:NSMakeRange(0, [outputStr length])];  44 45     46 47     return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  48 49 }

 

 

2.UTF-8和Unicode转化

 

 1 //Unicode转UTF-8 2  3 + (NSString*) replaceUnicode:(NSString*)aUnicodeString 4  5 { 6  7     NSString *tempStr1 = [aUnicodeString stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];   8  9     NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];  10 11     NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];  12 13     NSData *tempData =http://www.mamicode.com/ [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];  14 15     NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData  16 17                                                            mutabilityOption:NSPropertyListImmutable   18 19                                                                      format:NULL  20 21                                                            errorDescription:NULL];  22 23     24 25     return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"]; 26 27 }28 29  30 31  32 33 +(NSString *) utf8ToUnicode:(NSString *)string34 35 {36 37     NSUInteger length = [string length];38 39     NSMutableString *s = [NSMutableString stringWithCapacity:0];40 41     for (int i = 0;i < length; i++) 42 43     {44 45         unichar _char = [string characterAtIndex:i];46 47         //判断是否为英文和数字48 49         if (_char <= 9 && _char >= 0) 50 51         {52 53             [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];54 55         }56 57         else if(_char >= a && _char <= z)58 59         {60 61             [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];62 63             64 65         }66 67         else if(_char >= A && _char <= Z)68 69         {70 71             [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i, 1)]];72 73             75         }76 77         else78 79         {80 81             [s appendFormat:@"\\u%x",[string characterAtIndex:i]];82 83         }84 85     }86 87     return s;88 89 }

 

ios基本编码格式转换UTF8