如果源代码是 win32 中的 UTF-8 编码,如何显示 UTF-16 const 字符串?

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

我的程序需要显示一些 unicode 字符串。如果我使用 UTF-16LE 保存源代码,一切正常。但是,我无法使用 mingw 在 linux 中进行交叉编译。如果我使用 UTF-8 保存源代码,则可以毫无问题地编译源代码。但是所有 const unicode 字符串都没有正确显示,因为它们被编码为 UTF-8。源代码保存为UTF-8编码时,如何正确显示unicode字符串?

示例代码:

#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

经过一些测试,如果文件被编码为没有签名 (BOM) 的 UTF-8,则 visual studio 无法正确编码字符串。将编码方案更改为带签名的 UTF-8 后一切正常。