iOS中編碼轉化

來源:互聯網
上載者:User

原文地址: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;

}

 

by:panguo

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.