Windows uses a UTF-16, create a UTF-16 file set the first two bytes to 0 xfffe, which is the BOM code for the UTF-16

Source: Internet
Author: User
Unicode in Windows is a UTF-16, and each word is represented in two bytes. During programming, you can use the text macro and the Unicode and _ Unicode variables defined in the project to ensure that the entire project works under Unicode.

Some problems occur when writing strings to files. Write the file using the writefile function, the string itself is UTF-16, after writing the file found that with vim and notepad open are not correctly displayed. In hexadecimal notation, each word is correct and has two bytes. If it is an English letter, the second byte is 00.

Google found the answer. To write 0 xfffe at the beginning of the file, this is the identifier of the Unicode file. After reading this header in notepad and WordPad in Windows, you can correctly identify this Unicode file. Therefore, when creating a text file in the code, you need to write such a section (use logger. cpp of scheduledownload as an example ):

// Logfile doesn't exist, create it, that's all
Hfile = createfile (log_file_path, generic_write, null, null, create_new, file_attribute_normal, null );
If (hfile = invalid_handle_value ){
Operate_result = false;
} Else {
// Write 0 xfffe at the beginning of the file, this makes notepad reads unicodes well
Word unicode_identifier = 0 xfeff;
If (writefile (hfile, & unicode_identifier, sizeof (Word), & bytes_written, null )){
Operate_result = true;
} Else {
Operate_result = false;
}
}
Goto finished;

Finished:
If (hfile! = NULL & hfile! = Invalid_handle_value)
Closehandle (hfile );
Return operate_result;

 

Do not wonder why the value of the unicode_identifier variable is set to 0 xfeff. This is because x86 is little endian, so oxfeff in the Code is included in the register and then set to the memory, from the low address to the high address, it becomes fffe, so that after the word is written to the file, it is exactly fffe.

Finally, I tested it again, and there was no problem writing Chinese characters. If you use gvim to open the file, you need to set it. it is OK to open the file in Ubuntu. If gvim. vimrc in Windows is set to the same as Ubuntu, it should be okay.

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.