[Original] MBCS, Unicode, utf8, and a tool class for reading and writing utf8 files in C ++

Source: Internet
Author: User
There is nothing else to say, just look at it. Code You can, the only one that needs to be noted is that the three commonly used character encodings in C ++ are MBCS, Unicode, and utf8. New users are generally confused about this issue.
MBCS is a variable-length Byte encoding. The specific encoding is determined by the operating system, which is actually the internal code of the operating system. For example, in the simplified Chinese operating system, the encoding is gb18030 (the superset of gb2312); in the traditional Chinese operating system, the encoding is big5. For gb18030, the English character encoding is the same as the ASCII code value. It is a single-byte encoding, and a Chinese character is a dual-byte encoding. Its value is the corresponding location code value.
Unicode is double-byte encoding. The English character encoding is also the same as the ASCII code value. The difference is that even if English characters are expressed in double bytes, the first byte value is 0, and the second byte is the corresponding ASCII code value. The difference between Chinese characters and gb18030 is that the encoding value of the same Chinese character is different, not the location code value of the Chinese character, but the Unicode encoding value.
Utf8 is also a variable-length Byte encoding. Its English character encoding is also the same as the ASCII code value, but only uses one byte encoding. For Chinese characters and other characters, it uses three bytes encoding. The maximum length of a UTF-8 encoding is 4 bytes.

From the preceding descriptions, we can see that a text file with the same mix of Chinese and English is stored. The files encoded by MBCS and utf8 are smaller than those encoded by Unicode, this is because the first two pairs of English characters use single-byte encoding, which saves more space. The experiment also found that the size of the MBCS file is slightly smaller than that of the utf8 file, because the utf8 file requires more bytes to save Chinese characters.
Unicode encoding is more efficient than MBCS and utf8 encoding in terms of computer processing efficiency. This is because Unicode is a fixed-length encoding. In the processing process, you do not need to determine the starting position of the next character, so the processing efficiency is high. In C ++, if unicode encoding is enabled Program All characters in are unicode encoded. If you want to save it as utf8 encoding, you must manually process it.
The attachment is a tool class for reading and writing utf8 files in C ++. I want to find the C ++ Method for reading and writing utf8 on the Internet, and I hope to help later people. The cdirectory class in the attachment is the folder operation function I used in the project.

Code download

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.