iOS basic encoding format conversion UTF8

Source: Internet
Author: User

Encoding conversions in iOS

1.utf-8 Conversion

nsstring *data = @ " Hello, Beijing!" ";

convert into UTF-8

nsstring *datautf8 = [Data stringbyaddingpercentescapesusingencoding: nsutf8stringencoding];

NSLog (@ "%@", dataUTF8);

UTF-8 to GBK, replace the UTF8 code, the official explanation is as follows.

Replaces all percent escapes and 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 EN  Coding). See Cfurlcreatestringbyreplacingpercentescapes in CFURL.h for more complex transformations

nsstring *DATAGBK = [dataUTF8 stringbyreplacingpercentescapesusingencoding: nsutf8stringencoding];

NSLog (@ "%@", DATAGBK);

The results of the execution in Xcode4.2 are as follows:

Encapsulate the above method as follows:

1 //Unicode goto UTF-82 3  4 5+ (NSString *) encodetopercentescapestring: (NSString *) Input6 7 {  8 9     //Encode all the reserved characters, per RFC 3986Ten  One     //(<Http://www.ietf.org/rfc/rfc3986.txt>) A  -NSString *outputstr = (NSString *)    -  the cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault, -  - (cfstringref) input, -  + NULL, -  +(CFSTRINGREF)@"!* ' ();: @&=+$,/?%#[]",   A  at kCFStringEncodingUTF8);  -  -     returnOutputstr;  -  - }   -  in   -  to+ (NSString *) decodefrompercentescapestring: (NSString *) Input +  - {   the  *Nsmutablestring *outputstr =[nsmutablestring Stringwithstring:input];  $ Panax Notoginseng[Outputstr replaceoccurrencesofstring:@"+"   -  theWithstring:@" "   +  A Options:nsliteralsearch the  +Range:nsmakerange (0, [outputstr length])];  -  $      $  -     return[Outputstr stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];  -  the}

2.utf-8 and Unicode conversions

1 //Unicode goto UTF-82 3+ (nsstring*) Replaceunicode: (nsstring*) aunicodestring4 5 {6 7NSString *tempstr1 = [aunicodestring stringbyreplacingoccurrencesofstring:@"\\u"Withstring:@"\\u"]; 8 9NSString *TEMPSTR2 = [tempStr1 stringbyreplacingoccurrencesofstring:@"\ "" Withstring:@"\\\""]; Ten  OneNSString *TEMPSTR3 = [[@"\ "" STRINGBYAPPENDINGSTRING:TEMPSTR2] stringbyappendingstring:@"\""];  A  -NSData *tempdata =[TempStr3 datausingencoding:nsutf8stringencoding];  -  thensstring* Returnstr =[Nspropertylistserialization propertylistfromdata:tempdata -  - mutabilityoption:nspropertylistimmutable -  + Format:null -  + Errordescription:null];  A  at      -  -     return[Returnstr stringbyreplacingoccurrencesofstring:@"\\r\\n"Withstring:@"\ n"];  -  - } -  in   -  to   +  -+ (NSString *) Utf8tounicode: (NSString *)string the  * { $ Panax NotoginsengNsuinteger length = [stringlength]; -  thensmutablestring *s = [nsmutablestring stringwithcapacity:0]; +  A      for(inti =0; i < length; i++)  the  +     { -  $Unichar _char = [stringcharacteratindex:i]; $  -         //Judging whether it is English and digital -  the         if(_char <='9'&& _char >='0')  - Wuyi         { the  -[s AppendFormat:@"%@",[stringSubstringwithrange:nsmakerange (I,1)]]; Wu  -         } About  $         Else if(_char >='a'&& _char <='Z') -  -         { -  A[s AppendFormat:@"%@",[stringSubstringwithrange:nsmakerange (I,1)]]; +  the              -  $         } the  the         Else if(_char >='A'&& _char <='Z') the  the         { -  in[s AppendFormat:@"%@",[stringSubstringwithrange:nsmakerange (I,1)]]; the  the              the         } the  the         Else +  -         { the Bayi[s AppendFormat:@"\\u%x",[stringCharacteratindex:i]]; the  the         } -  -     } the  the     returns; the  the}

iOS basic encoding format conversion UTF8

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.