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;
}