Chinese character ASCII code and NSString Conversion

Source: Internet
Author: User

In xcode, files are saved in utf8 format. Therefore, the variable object is also saved in utf8 format. Different Languages have different utf8 codes. English utf8 codes are the same as ascii codes. The number of UTF-8 encoded bytes for each character in different languages is different, and the bytecode is also different. For English characters, It is very convenient to view its ascii code. It is its ascii code. In fact, for non-English characters, the same is true for character set encoding. This is collectively referred to as the ASCII code, which is also described in many documents. There are many examples on the Internet to illustrate how to convert characters and ASCII codes. However, they do not mention how to convert Chinese characters and other non-English characters. Use the English conversion test, as shown below: // NSString to ASCIINSString * string = @ "A"; int asciiCode = [string characterAtIndex: 0]; // 65 // ASCII to NSStringint asciiCode = 65; NSString * string = [NSString stringWithFormat: @ "% c", asciiCode]; // A test it in Chinese, [NSString stringWithFormat: @ "% c", asciiCode] is garbled characters, that is, they are not correctly identified. Before proceeding to the solution, let's take a look at the various formats in the stringWithFormat method. There are two formats for converting the ascii code into characters: % c and % C. /* % C 8-bit unsigned character (unsigned char), printed by NSLog () as an ASCII character, or, if not an ASCII character, in the octal format \ ddd or the Unicode hexadecimal format \ udddd, where d is a digit. % C 16-bit Unicode character (unichar), printed by NSLog () as an ASCII character, or, if not an ASCII character, in the octal format \ ddd or the Unicode hexadecimal format \ udddd, where d I S a digit. */use [NSString stringWithFormat: @ "% C", asciiCode] to get the desired character. Take English, Chinese, and Japanese as examples. NSString * theString = @ "g"; unichar theChar = [theString characterAtIndex: 0]; NSString * theString1 = [NSString stringWithFormat: @ "% c", theChar]; NSString * theString2 = [NSString stringWithFormat: @ "% C", theChar]; NSLog (@ "theString = % @, % d, % @, % @", theString, theChar, theString1, theString2); theString = @ ""; theChar = [theString characterAtIndex: 0]; theString1 = [NSString stringWithFormat: @ "% c", theChar]; TheString2 = [NSString stringWithFormat: @ "% C", theChar]; NSLog (@ "theString = % @, % d, % @, % @", theString, theChar, theString1, theString2); theString = @ "character"; theChar = [theString characterAtIndex: 2]; theString1 = [NSString stringWithFormat: @ "% c", theChar]; theString2 = [NSString stringWithFormat: @ "% C", theChar]; NSLog (@ "theString = % @, % d, % @, % @", theString, theChar, theString1, theString2); 2013-09-12 15: 36: 27.849 XYShopping [1892: 18e03] theString = g, 103, g, g2013-09-12 15:36:27. 849 XYShopping [1892: 18e03] theString = home, 23478 ,?, Jia 2013-09-12 15:36:27. 849 XYShopping [1892: 18e03] theString = too many bytes delayed, 12488 ,?, The result of explain shows that this method is correct. The two-byte characters can be displayed. I don't know what other languages will do. I have no conditions to test it.

Related Article

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.