Search code examples
cwinapimingwencode

How to display UTF-16 const strings if the source code is UTF-8 encoded in win32?


My program need to display some unicode strings. If I save the source code using UTF-16LE, everything works fine. However, I was not be able to cross compile in linux using mingw. If I save the source using UTF-8, the source code can be compiled with no problem. But all the const unicode strings were not displayed correctly since they are encoded as UTF-8. How can I properly display unicode string when the source code is saved as UTF-8 encoded?

Example Code:

#include <Windows.h>

int main(int argc, const char *argv[])
{
    MessageBoxW(NULL, L"你好", L"你好", MB_OK);
}

Compiled with UTF-16LE source file

Compiled with UTF-8 source file


Solution

  • After some testing, it turns out visual studio cannot encode string correctly if the file is encoded to UTF-8 without signature(BOM). Everything works fine after changing encoding scheme to UTF-8 with signature.