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.