如何使用 Visual Studio 将 Unicode 打印到 C 中的输出控制台? [英] How do I print Unicode to the output console in C with Visual Studio?

查看:40
本文介绍了如何使用 Visual Studio 将 Unicode 打印到 C 中的输出控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As the question says, do I have to do in order to print Unicode characters to the output console? 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;

并打印:文本是?.

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

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.

那我该怎么办?

推荐答案

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

This is code that works for me (VS2017) - project with Unicode enabled

#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);
}

这是控制台

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

After copying it to the Notepad++ I see the proper string

来了.测试 unicode -- 英语 -- Ελληνικά -- Español.

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

OS - Windows 7 English, Console font - Lucida Console

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

I tried to fix the above code to work with VS2019 on Windows 10 and best I could come up with is this

#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

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

When it is run with console set to Lucida Console fond and UTF-8 encoding I see

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

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

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

When text is copied from the last console to Notepad++ all characters are shown correctly

这篇关于如何使用 Visual Studio 将 Unicode 打印到 C 中的输出控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆