Encoding and conversion in IOS

Source: Internet
Author: User

Address: http://blog.csdn.net/huifeidexin_1/article/details/7883984

Encoding and conversion in IOS

1. Conversion of UTF-8

 

Nsstring * Data = @ "Hello, Beijing! ";

// Convert to UTF-8

Nsstring * datautf8 = [datastringbyaddingpercentescapesusingencoding: nsutf8stringencoding];

Nslog (@ "% @", datautf8 );

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

// 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 );

 

The execution result in xcode4.2 is as follows:

Encapsulate the preceding method as follows:

 

// Unicode-to-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 and Unicode Conversion

 

// Unicode-to-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];

// Determine whether it is an English or a number

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

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.