简体   繁体   English

Unicode无法正确输出(Windows 10)

[英]Unicode doesn't output correctly (Windows 10)

I am a beginner programmer and I'm working on a simple game. 我是一个初学者,正在开发一个简单的游戏。 I want to output Unicode symbols like Alt + 2 1 9 █ to the console. 我想像ALT + 2 1 9█到控制台输出的Unicode符号。 I am using CodeBlocks on Windows 10 and I am using GNU GCC compiler. 我正在Windows 10上使用CodeBlocks,并且正在使用GNU GCC编译器。 Here is what I'm trying to do: 这是我想要做的:

#include <iostream>

using namespace std;

int main()
{
    cout << "\u2588";
    return 0;
}

What I get on console is a few strange symbols. 我在控制台上看到的是一些奇怪的符号。 This happens even with other Unicode characters, but the symbols are different. 即使使用其他Unicode字符,也会发生这种情况,但是符号不同。

I tried using wcout , but then i get just a blank space instead. 我尝试使用wcout ,但是我得到的只是空白。

I also tried things like this: 我也尝试过这样的事情:

_setmode(_fileno(stdout), _O_U16TEXT);

But then I get errors that _fileno and _O_U16TEXT was not declared, though I imported the libraries that seem to be necessary for this: 但是然后我得到了错误的_fileno ,尽管我导入了对此似乎必需的库,但未声明_fileno_O_U16TEXT

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

The Unicode symbols are not totally necessary for my game, but I just want it look nicer. Unicode符号并不是我的游戏完全必需的,但我只是希望它看起来更好。

That's because you're trying to print wchar_t (16 bits long) characters with std::cout that only supports char (8 bits long). 那是因为您尝试使用仅支持char (8位长)的std::cout打印wchar_t (16位长)字符。 Use std::wcout instead of std::cout , wcout is meant for wide characters. 使用std::wcout代替std::coutwcout用于宽字符。

The Windows Command Prompt uses CP850 as encoding by default, and won't be able to correctly display this symbol or any Unicode symbol for that matter unless you change it . Windows命令提示符默认情况下使用CP850作为编码, 除非对此进行更改,否则将无法正确显示该符号或任何Unicode符号。

It has, however, its own version of it, \\xDB that is probably what you are looking for. 但是,它具有它自己的版本, \\xDB可能正是您所需要的。

For other symbols you can use in the Windows Command Prompt, open charmap.exe and check the font Terminal , there's a range of useful symbols that should be compatible with CP850 and display under the Command Prompt the same way they are presented there. 对于其他符号,您可以在Windows命令提示符中使用,打开charmap.exe并检查字体Terminal ,有一系列有用的符号应与CP850兼容,并在命令提示符下以与它们相同的方式显示。

Other answers have addressed treating the string as a UTF-16 literal. 其他答案已解决将字符串视为UTF-16文字。 You can also tell the compiler to treat it as a utf-8 literal (forcing it to encode all characters within it as utf-8) by prefixing it with u8 : 您还可以通过在编译器前面加上u8前缀来告诉编译器将其视为utf-8文字(强制其将其中的所有字符编码为utf-8):

#include <iostream>

using namespace std;

int main()
{
    cout << u8"\u2588";
    return 0;
}

Note that windows consoles aren't by default set to treat output as utf-8, so this still won't display correctly without some additional settings. 请注意,默认情况下未将Windows控制台设置为将输出视为utf-8,因此,如果没有一些其他设置,该控制台仍无法正确显示。

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

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