IOS basic encoding format conversion

Source: Internet
Author: User
Tags rfc

Encoding and conversion in IOS

1. Conversion of UTF-8

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

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

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 = [nsmutablestring
Stringwithstring: Input];

[Outputstr replaceoccurrencesofstring: @ "+"

Withstring :@""

Options: nsliteralsearch

Range: nsmakerange (0, [outputstr
Length])];

Return [outputstr
Stringbyreplacingpercentescapesusingencoding: nsutf8stringencoding];

}

2. UTF-8 and Unicode Conversion

// Unicode-to-UTF-8

+ (Nsstring *) replaceunicode :( nsstring *) aunicodestring

{

Nsstring * tempstr1 = [aunicodestring
Stringbyreplacingoccurrencesofstring: @ "\ U"
Withstring: @ "\ U"];

Nsstring * tempstr2 = [tempstr1
Stringbyreplacingoccurrencesofstring :@"\""
Withstring: @ "\" "];

Nsstring * tempstr3 = [[@"\""
Stringbyappendingstring: tempstr2] stringbyappendingstring: @ "\" "];

Nsdata * tempdata = [tempstr3
Datausingencoding: nsutf8stringencoding];

Nsstring * returnstr = [nspropertylistserialization
Propertylistfromdata: tempdata


Mutabilityoption: nspropertylistimmutable


Format: NULL


Errordescription: NULL];

Return [returnstr
Stringbyreplacingoccurrencesofstring: @ "\ r \ n"
Withstring: @ "\ n"];

}

+ (Nsstring *) utf8tounicode :( nsstring *) String

{

Nsuinteger length = [String
Length];

Nsmutablestring * s = [nsmutablestring
Stringwithcapacity: 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: @ "% @", [String
Substringwithrange: nsmakerange (I,
1)];

}

Else if (_ char> =
'A' & _ char <= 'Z ')

{

[S appendformat: @ "% @", [String
Substringwithrange: nsmakerange (I,
1)];

}

Else if (_ char> =
'A' & _ char <= 'Z ')

{

[S appendformat: @ "% @", [String
Substringwithrange: nsmakerange (I,
1)];

}

Else

{

[S appendformat: @ "\ U % x", [String
Characteratindex: I];

}

}

Return S;

}

Related Article

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.