The difference among ANSI, MBCS with Unicode

Source: Internet
Author: User
Unicode is often introduced in programming books, and various formats are often used in string functions. The following uses "ABC me and you" as an example to describe Unicode.

Test source code:

Tchar * P = _ T ("ABC me you him ");
STD: cout <_ tcsclen (P );

We wroteCodeIt should look like this, that is, code that conforms to both ANSI and Unicode, using the _ t macro and _ TS function set.

I. ANSI encoding: one common English letter, two bytes of Chinese characters. "ABC me you" occupies 9 bytes.
 
There are no related options in the compilation options.
Output 9
In this case, _ tcsclen is strlen and the result is the number of bytes.

Use the multi-character function, that is, the multibyte function. Set _ MBCS in the compilation options, the default project generated by this option VC contains (Project-> setting-> C ++-> Preprocessor setting ).
In this case, _ tcsclen is _ mbslen. The output value is 6, which actually occupies 9 bytes.

2. Unicode encoding. The so-called wide characters, English characters and Chinese characters both occupy 2 bytes. In the compilation options, SET _ Unicode and Unicode.
Source code is equivalent to wchar_t * P = l "ABC me you him ";
STD: cout <wcslen (P );
The result is 6, which actually occupies 12 bytes .;

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.