NSString Mutual transformation of UTF-8 and GBK in Object-c in iOS development

Source: Internet
Author: User

Application all have to encounter a very headache problem: text encoding, Chinese character's GBK and the international common UTF-8 of mutual transformation slightly a careless, will be full screen garbled. The following is a description of the UTF-8 and GBK NSString Mutual transformation methods

Nsstringencoding enc = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);

char* c_test = "Beijing";

int nlen = strlen (c_test);

nsstring* str = [[NSString alloc]initwithbytes:c_test Length:nlen Encoding:enc];

NSLog (@ "str =%@", str);

go from GBK to UTF-8With Nsstringencoding enc =cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000), Then you can use initwithdata:encoding to achieve.go from UTF-8 to GBKCfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000), ENC is kcfstringencodinginvalidid. Never mind, try NSData *data=[nsstring datausingencoding:-2147482063]; Conversion character encoding is mainly used in the Cfstringconvertencodingtonsstringencoding function, you can see the use of this function nsstringencoding enc =  Cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000); The complete code is as follows:Nsurl *url = [Nsurl urlwithstring:urlstr];NSData *data = [NSData Datawithcontentsofurl:url];nsstringencoding enc = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);nsstring *retstr = [[NSString alloc] Initwithdata:data Encoding:enc];A more convenient conversion nsstring for UTF8 encoded functions, you can tryheader file:@interface NSString (oaurlencodingadditions)-(NSString *) urlencodedstring; -(NSString *) urldecodedstring; @endm File:@implementation NSString (oaurlencodingadditions)-(NSString *) urlencodedstring{nsstring *result = (NSString *) CFURLCr Eatestringbyaddingpercentescapes (Kcfallocatordefault, (cfstringref) self,null,cfstr ("!* ' ();: @&=+$,/?%#[]"), KCFSTRINGENCODINGUTF8); [Result Autorelease]; return result; }-(nsstring*) urldecodedstring{nsstring *result = (NSString *) Cfurlcreatestringbyreplacingpercentescapesusingencoding (Kcfallocatordefault, (cfstringref) Self, CFSTR (""), KCFSTRINGENCODINGUTF8);    Cfstr (""), kCFStringEncodingUTF8);       [Result Autorelease]; return result;  } @end If you need to convert a nsstring, you only need nsstring *temp = [@ "Test UTF8" urlencodedstring]; NSString *decoded = [temp urldecodedstring];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.