Relationship and differences between Unicode and multi-byte (note)

Source: Internet
Author: User

We all know that Microsoft uses two character set encoding formats: unicode encoding and multi-byte encoding. What is the relationship between them? Okay. Let's take a look at the following program:

Tchar * P = _ T ("IT learning ");
// Multi-Byte Character Set
Int A = sizeof (p); // = ???
Int B = _ tcslen (p); // = ???;
// Under the Unicode Character Set
Int c = sizeof (p); // = ???
Int d = _ tcslen (p); // = ???

 

This question can already reflect the difference between Unicode and multi-byte. First, the _ tcslen function is a macro defined as follows:

# Ifdef _ Unicode
# DEFINE _ tcslen wcslen

# Else

# DEFINE _ tcslen strlen

# Endif

 

The above statement is clear, that is:

When _ Unicode macro is not defined, tchar = char, _ tcslen = strlen when _ Unicode macro is defined, tchar = wchar_t, _ tcslen = wcslen because P is a pointer type, that is to say, the address variable is 4 bytes on a 32-bit machine, so both A and C are 4. Multi-byte means that the string can be one byte or two bytes, only one byte can be used for English characters, but two Chinese characters are required (except for uncommon characters). Therefore, in the case of multiple bytes, B is 6 (I occupies one byte and T occupies one byte, ("") two Chinese characters each occupy two bytes.) The end character of the string is not counted because only the valid length is calculated. In the Unicode environment, D is 4. (because _ tcslen is the function wcslen, two bytes are taken at a time, that is, one length represents two bytes) at this time, the string end flag requires two '\ 0' representation. If not all two end marks are used, the computer determines that the string has not ended. Therefore, the calculation process is as follows: the letter 'I' occupies two bytes. The first one is '\ o', the second one is' I ', and the other is T. The 'learn' occupies two bytes respectively, that is, two lengths, so the string length is 4 at this time. Please correct me if I am not writing a blog at the beginning ~~~~~

Appendix:

Generally we say Chinese characters are occupying 2 bytes (GB-2312 ).
The Chinese character encoding formats are different:
The GB-231280 is encoded into 2 bytes (byte) containing 20902 Chinese characters in the range of 0x8140-0xfefe.
On the basis of GBK, GB18030-2000 (gbk2k) further expands Chinese characters and adds the fonts of Tibetan and Mongolian ethnic minorities. The encoding is variable, and the second part is compatible with GBK. The four-byte part is the expanded font and character bit, the encoding range is the first byte 0x81-0xfe, the second byte 0x30-0x39, the three byte 0x81-0xfe, and the four byte 0x30-0x39.

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.