如何使用 Visual Studio 将 Unicode 打印到 C 中的输出控制台?

Eär*_*ins 4 c windows unicode visual-studio

正如问题所说,我必须做什么才能将 Unicode 字符打印到输出控制台?我必须使用哪些设置?现在我有这个代码:

wchar_t* text = L"the ?";
wprintf(L"Text is %s.\n", text);
return EXIT_SUCCESS;
Run Code Online (Sandbox Code Playgroud)

它打印: Text is the ?.

我尝试将输出控制台的字体更改为 MS Mincho、Lucida Console 和其他一些字体,但它们仍然不显示日文字符。

那么,我该怎么办?

Art*_*sky 6

这是对我有用的代码 (VS2017) - 启用 Unicode 的项目

#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    wchar_t * test = L"the ?. Testing unicode -- English -- ???????? -- Español." ;

    wprintf(L"%s\n", test);
}
Run Code Online (Sandbox Code Playgroud)

这是控制台

输出

将其复制到 Notepad++ 后,我看到了正确的字符串

这 ?。测试 unicode -- 英语 -- ???????? ——西班牙语。

操作系统 - Windows 7 英文,控制台字体 - Lucida 控制台

基于评论的编辑

我试图修复上面的代码以在 Windows 10 上与 VS2019 一起工作,我能想到的最好的就是这个

#include <stdio.h>
int main()
{
    const auto* test = L"the ?. Testing unicode -- English -- ???????? -- Español.";

    wprintf(L"%s\n", test);
}
Run Code Online (Sandbox Code Playgroud)

当它“按原样”运行时,我看到 默认控制台设置

当它在控制台设置为喜欢 Lucida Console 和 UTF-8 编码的情况下运行时,我看到 控制台切换到 UTF-8

作为答案?字符显示为空矩形 - 我想是不包含所有 Unicode gliphs 的字体的限制

当文本从最后一个控制台复制到 Notepad++ 时,所有字符都正确显示


小智 5

字符“\xe6\x9d\xa5\”可能不在您的系统字符代码页中。您需要将字符保存为utf-8。

\n\n

在 vs2013 中,我尝试这样做:

\n\n
// save as utf-8\n#pragma execution_character_set( "utf-8" )\n\n#include <Windows.h>\n\nchar *s = "the \xe6\x9d\xa5";\n\nint main(){\n    // set console code page to utf-8\n    SetConsoleOutputCP(65001);\n    printf("%s\\n",s);\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n