iOS encoding Conversion

Source: Internet
Author: User

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

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.