Solution: 1. When the IOS Mobile Phone obtains Chinese characters in the background, garbled characters are displayed: Resolve the obtained GBK-encoded characters to normal Chinese characters: // declare a GBK encoding type nsstringencoding gbkencoding = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000); // use the following method to encode the obtained data by gbkencoding, the result will be a normal Chinese Character nsstring * Records = [[nsstring alloc] initwithdata: huoqudaodedata encoding: gbkencoding]; example: This project uses the asiformdatarequest tool to parse the data obtained from the background, therefore, set asihttprequest. the following method is used in the M file:-(NS) String *) responsestring {nsdata * Data = [self responsedata]; If (! Data) {return nil;} return [[nsstring alloc] initwithbytes: [data bytes] length: [Data Length] encoding: [self responseencoding] autorelding];} replace with-(nsstring *) responsestring {nsdata * Data = [self responsedata]; If (! Data) {return nil;} // indicates that nsstringencoding gbkencoding = encoding (kcfstringencodinggb_18030_2000) is decoded using gbkencoding; return [[nsstring alloc] initwithbytes: [data bytes] length: [Data Length] encoding: gbkencoding];} at that time, we will use the following method to request or send Chinese characters. No problem will occur: // The public method of this class is used to obtain data through the specified URL. + (Nsstring *) scheme :( nsstring *) URL {nsstring * encodedurl = [URL scheme: Scheme]; required * asihttprequest = [asiformdatarequest requestwithurl: [nsurl urlwithstring: encodedurl]; nsstringencoding ENC = cfstringconvertencodingtonsstringencoding (kcfstringencodingutf8); [asihttprequest setstringencoding: ENC]; [asihttprequest startsy Nchronous]; asihttprequest. delegate = self; nsstring * strresult = [asihttprequest responsestring]; // The above modification is to return strresult;} refer: GBK to UTF-8 (the above example can be used as a success case) with nsstringencoding ENC = fuse (kcfstringencodinggb_18030_2000), then you can use initwithdata: encoding to implement. From the UTF-8 to gbkcfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000), the obtained ENC is kcfstringencodinginvalidid. It doesn't matter. Try nsdata * Data = [nsstring datausingencoding:-2147482063]. Note: The kcfstringencodinggb_18030_2000 character set must be used. The kcfstringencodinggb_2312_80 character set cannot be used either.
Success stories: Convert str_selectedleixing, a string in uif-8 format (xcode's default), to strgbk in GBK format, as shown below:
Nsdata * Data = [str_selectedleixing datausingencoding:-2147482063];
Nsstring * strgbk = [[nsstring alloc] initwithdata: data encoding:-2147482063];
You do not need to add any additional development kits for the implementation of the above Code.
Nsstring, Char, and nsdata of iPhone convert each other. nsstring to Unicode string :( nsstring *) fname = @ "test"; char fnamestr [10]; memcpy (fnamestr, [fname cstringusingencoding: nsunicodestringencoding], 2 * ([fname length]); 2. nsstring to Char (nsstring *) fname = @ "test"; char fnamestr [10]; fnamestr = [fname utf8string]; 3. char-> nsdata: Method 1: char * postdata = "test"; nsdata * Data = [nsdata datawithbytes: postdata length: strlen (postdata)]; Method 2: Convert to nsstring: -(ID) initwithuf8string :( const char *) bytes and then use nsstring-(nsdata *) datausingencoding :( nsstringencoding) encoding4. nsdata-> char nsdata returndata; char * bu = [returndata bytes]; // use the following two methods to convert an nsstring from GBK to utf8. nsdata-> nsstringnsstring * astr; astr = [[nsstring alloc] initwithdata: aData encoding: nsasciistringencoding]; 6. nsstring-> nsdatansdata * aData; aData = [astr datausingencoding: nsasciistringencoding]; reference: http://blog.sina.com.cn/s/blog_4adf31ea0100t4y7.html
Http://www.devdiv.com/article-1397-1.html