Original address: http://blog.csdn.net/huifeidexin_1/article/details/7883984
Encoding conversions in iOS
1.utf-8 Conversion
nsstring *data = @ " Hello, Beijing!" ";
convert into 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 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 = [datautf8stringbyreplacingpercentescapesusingencoding: nsutf8stringencoding];
NSLog (@ "%@", DATAGBK);
The results of the execution in Xcode4.2 are as follows:
Encapsulate the above method as follows:
Unicode Goto 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 conversions
Unicode Goto 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];
Judging whether it is English and digital
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;
}
iOS encoding Conversion