Conversion of Chinese character and Unicode code in C + + __c++

Source: Internet
Author: User

The Unicode code (for example, 20319) of a Chinese character can be obtained by using the following methods:

1, declaring a 3-byte long array of characters, such as Char a[3];

2, assign a value to the array char a[3]={unsigned ( -20319)/256,unsigned (-20319)%256,0};

3, the output can be directly cout<<a<<endl;;

4, you can also assign a character array directly to string strings, such as String Str (a);.

The following is an explanation of step 2, with a Unicode code of 16 bits (unsigned short) and a character char type of only 8 digits, and the function of step 2 is to store the first 8 bits of Unicode and the latter 8 bits in a different 2 byte of a character array, which can be imagined, This should also be true for char-type strings that store Chinese characters. The end of 0 indicates the end of the array.

It is simpler to check the Unicode code of a Chinese character, declare a wchar_t variable, and directly assign a Chinese character, such as wchar_t ch= ' ah ', and direct cout<<ch<<endl; when displayed. However, a warning may be given at compile time. The output file is the same as normal operation.

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.