iOS development--Encoding format

Source: Internet
Author: User

1.url encoding

When HTTP requests in iOS encounter Chinese characters, they need to be converted to UTF-8, using the following methods:

NSString * encodingstring = [urlstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

The result is%3a%2f%2f;

2.url decoding Chinese

After the request, the returned data, if shown is the format:%3a%2f%2f, this time we need to do UTF-8 decoding, the method used is:

NSString *str = [Model.album_name stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];

UTF-8 Turn GBK

The result is Chinese

3.Unicode encoding and conversion of Chinese characters

This code is typically used for network programming. The data obtained from the server is typically a Unicode format string that needs to be converted to Chinese encoding correctly.

NSString a string encoding (such as \u7e8c) in Unicode format converted to Chinese

Unicode encoding starts with \u

+ (NSString *) Replaceunicode: (NSString *) unicodestr

{

NSString *TEMPSTR1 = [unicodestrstringbyreplacingoccurrencesofstring:@ "\\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];

NSLog (@ "%@", returnstr);

return [returnstrstringbyreplacingoccurrencesofstring:@ "\\r\\n" withstring:@ "\ n"];

}

\u6211\u662f\u4e2d\u6587\u5b57\u7b26
\uxxxx This format is a Unicode notation that represents a character in which XXXX represents a 16-digit, range-0~65535. Unicode hexadecimal numbers can only contain numeric 0~9, uppercase letters A~F, or lowercase a~f. It is important to note that the size-to-end problem of Unicode, which is usually small-ended, such as \u5c0f, which represents the ' small ' word in Chinese, is converted to 10 binary is 9215, so the byte array should be 1592.
Kanji to Unicode
Privatestring Unicode2chinese (String strunicode) {string[] splitstring=NewString[1]; splitstring[0] = "\\u"; String[] Unicodearray=strunicode.split (splitstring, stringsplitoptions.removeemptyentries); StringBuilder SB=NewStringBuilder (); foreach (string item in Unicodearray) {byte[] codes =New byte[2]; intCode1, Code2; Code1= Convert.ToInt32 (item. Substring (0, 2), 16); Code2= Convert.ToInt32 (item. Substring (2), 16); codes[0] = (byte) Code2;//must be small end in frontCODES[1] = (byte) Code1; Sb.    Append (Encoding.Unicode.GetString (codes)); }    returnsb. ToString ();}
Unicode to Kanji
Private string Chinese2unicode (String Strchinese) {    = string. Empty;    foreach (char  item in Strchinese.tochararray ())    {        + = "\\u" + (int//  16 binary     }    return  strunicodes;}

iOS development--Encoding format

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.