将Unicode输出到控制台的最佳方法是什么? [英] What is the best way to output Unicode to console?

查看:132
本文介绍了将Unicode输出到控制台的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio 2019中使用C ++ 17。我已经阅读了一些有关编码的文章,但是我对它们仍然不太满意。我想将UNICODE字符输出到屏幕。为此,我使用以下代码

I am working with C++17 in Visual Studio 2019. I have read a fair bit about encodings but I am still not very comfortable with them. I want to output UNICODE characters to screen. For that, I am using the following code

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

std::wstring symbol{ L"♚" };

_setmode(_fileno(stdout), _O_WTEXT);
std::wcout << symbol; //This works fine
std::cout << "Hello"; //This gives "Debug Assertion Failed! Expression: buffer_size % 2 == 0"
_setmode(_fileno(stdout), O_TEXT); //Need this to make std::cout work normally
std::cout << "World"; //This works fine

所以我可以将setmode设置为_O_WTEXT,然后每次需要时都返回O_TEXT输出std :: wstring。但是,我担心这可能是一种低效的处理方式。有更好的方法吗?我已经阅读了有关C ++中本机Widechar支持的内容,但是我很难理解。有人可以照亮我吗?

So I could do setmode to _O_WTEXT and then back to O_TEXT everytime I need to output the std::wstring. However, I am worried this may be an inefficient way to do things. Is there a better way to do it? I have read about something called native widechar support in C++ but I found it hard to understand. Could anyone illuminate me?

编辑

要添加到上面,使用尝试使用 std :: cout _setmode(_fileno(stdout),_ O_U16TEXT)导致与上述相同的行为>无需重新设置模式。如果我改用 _setmode(_fileno(stdout),_ O_U8TEXT),我的代码将无法编译并给出错误'symbol':重新定义,不同的基本类型'<<':使用 std :: cout 时对于班级是非法的在 std :: string符号=<插入我在下面的代码段中尝试过的所有可能性>

To add to the above, using _setmode(_fileno(stdout),_O_U16TEXT) leads to the same behaviour as described above when trying to use std::cout without setting the mode back. If I use _setmode(_fileno(stdout),_O_U8TEXT) instead, my code fails to compile and gives errors 'symbol': redefinition, different basic types and '<<': illegal for class when using std::cout on std::string symbol = <insert any of the possibilities I tried in the snippet below>.

建议使用UTF-8 std :: string 来使用 std :: cout 和这样就不必切换到宽屏模式。谁能帮我实现这一目标?我已经尝试过

I have been suggested to use a UTF-8 std::string to be able to use std::cout and that way avoid having to switch to wide mode. Could anyone give me a hand on how to achieve this? I have tried

std::string symbol = "\u265A"; //using std::cout gives "?" and triggers warning *
std::string symbol = "♚"; //Same as above

std::string symbol = u8"\u265A"; //using std::cout gives "ÔÖÜ"
std::string symbol = u8"♚"; //Same as above

* 相同严重性代码说明项目文件行抑制状态
在当前代码页(1252)中无法表示由通用字符名称'\u265A'表示的C4566字符

我已经读过,有可能使用以下命令将 std :: wstring 转换为UTF-8 std :: string 标头< Windows.h> 中的WideCharToMultiByte()。那行得通吗?有人可以提供任何帮助吗?

I have read it may be possible to convert from std::wstring to UTF-8 std::string using WideCharToMultiByte() from the header <Windows.h>. Would that work? Could anyone offer any help?

推荐答案

提示在错误消息中。 ...无法在当前代码页中表示(1252)。因此,代码页需要更改。 UTF-8的代码页标识符是65001。要更改代码页,请使用 SetConsoleOutputCP

The clue is in the error message. "...cannot be represented in the current code page (1252)". So the code page needs to be changed. The code page identifier for UTF-8 is 65001. To change the code page, use SetConsoleOutputCP.

这篇关于将Unicode输出到控制台的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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