简体   繁体   English

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

[英]How do I print Unicode to the output console in C with Visual Studio?

As the question says, do I have to do in order to print Unicode characters to the output console?正如问题所说,我必须做什么才能将 Unicode 字符打印到输出控制台? And what settings do I have to use?我必须使用哪些设置? Right now I have this code:现在我有这个代码:

wchar_t* text = L"the 来";
wprintf(L"Text is %s.\n", text);
return EXIT_SUCCESS;

and it prints: Text is the ?.它打印: Text is the ?.

I've tried to change the output console's font to MS Mincho, Lucida Console and a bunch of others but they still don't display the japanese character.我尝试将输出控制台的字体更改为 MS Mincho、Lucida Console 和其他一些字体,但它们仍然不显示日文字符。

So, what do I have to do?那么,我该怎么办?

This is code that works for me (VS2017) - project with Unicode enabled这是对我有用的代码 (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);
}

This is console这是控制台

输出

After copying it to the Notepad++ I see the proper string将其复制到 Notepad++ 后,我看到了正确的字符串

the 来.来。 Testing unicode -- English -- Ελληνικά -- Español.测试 unicode -- 英语 -- Ελληνικά -- 西班牙语。

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

Edits based on comments基于评论的编辑

I tried to fix the above code to work with VS2019 on Windows 10 and best I could come up with is this我试图修复上面的代码以在 Windows 10 上与 VS2019 一起工作,我能想到的最好的就是这个

#include <stdio.h>
int main()
{
    const auto* test = L"the 来. Testing unicode -- English -- Ελληνικά -- Español.";

    wprintf(L"%s\n", test);
}

When run it "as is" I see当它“按原样”运行时,我看到默认控制台设置

When it is run with console set to Lucida Console fond and UTF-8 encoding I see当它在控制台设置为喜欢 Lucida Console 和 UTF-8 编码的情况下运行时,我看到控制台切换到 UTF-8

As the answer to 来 character shown as empty rectangle - I suppose is the limitation of the font which does not contain all the Unicode gliphs作为显示为空矩形的来字符的答案 - 我想是不包含所有 Unicode gliphs 的字体的限制

When text is copied from the last console to Notepad++ all characters are shown correctly当文本从最后一个控制台复制到 Notepad++ 时,所有字符都正确显示

A question mark usually means Windows was unable to convert the character to the destination codepage.问号通常表示 Windows 无法将字符转换为目标代码页。 In the console a hollow square means the Unicode character was received correctly but it could not be displayed because the console font does not support it or it is a complex script requiring Uniscribe which the console does not handle.在控制台中,空心方块表示 Unicode 字符已正确接收但无法显示,因为控制台字体不支持它,或者它是一个需要 Uniscribe 的复杂脚本,控制台无法处理。 You can copy the square and paste it in Notepad/Wordpad and it should display correctly.您可以复制正方形并将其粘贴到记事本/写字板中,它应该可以正确显示。

The WriteConsoleW Windows function can display Unicode characters and works all the way back to Windows NT. WriteConsoleW Windows 函数可以显示 Unicode 字符,并且可以一直工作到 Windows NT。 It can only write to the console so you must use WriteFile instead when the output is redirected.它只能写入控制台,因此在重定向输出时必须改用WriteFile GetConsoleMode fails on redirected handles. GetConsoleMode在重定向句柄上失败。

You don't say which VS version you are using and things have changed over the years but Unicode output has been decent since VS2005 if you call _setmode(_fileno(stdout), _O_U16TEXT);你没有说你使用的是哪个 VS 版本,而且这些年来情况发生了变化,但是如果你调用_setmode(_fileno(stdout), _O_U16TEXT); Unicode 输出自 VS2005 以来一直不错_setmode(_fileno(stdout), _O_U16TEXT); early in main():在 main() 早期:

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

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT); // Call this before writing anything

    wchar_t * test = L"the 来" ;
    wprintf(L"Text is %s.\n", test);
    return 0;
}

See also: Myth busting in the console另请参阅: 控制台中的神话破坏

The characters '来' may not be in your system character code page.字符“来”可能不在您的系统字符代码页中。 You need to save the characters as utf-8.您需要将字符保存为 utf-8。

in vs2013, I try this:在 vs2013 中,我试试这个:

// save as utf-8
#pragma execution_character_set( "utf-8" )

#include <Windows.h>

char *s = "the 来";

int main(){
    // set console code page to utf-8
    SetConsoleOutputCP(65001);
    printf("%s\n",s);
    return 0;
}

This is what worked for me:这对我有用:

#include <locale.h>

and in the main function,在主函数中,

setlocale(LC_ALL, "en_US.UTF-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM