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