将char32_t打印到控制台 [英] Print char32_t to console
问题描述
如何在C ++ 11中打印(cout
/wcout
/...)char32_t到控制台?
How can I print (cout
/ wcout
/ ...) char32_t to console in C++11?
以下代码显示十六进制值:
The following code prints hex values:
u32string s2 = U"Добрый день";
for(auto x:s2){
wcout<<(char32_t)x<<endl;
}
推荐答案
首先,我认为除了char
和wchar_t
之外,wcout
不应被打印为字符. char32_t
都不是.
First, I don't think wcout
is supposed to print as characters anything but char
and wchar_t
. char32_t
is neither.
这是一个示例程序,可打印单个wchar_t's
:
Here's a sample program that prints individual wchar_t's
:
#include <iostream>
using namespace std;
int main()
{
wcout << (wchar_t)0x41 << endl;
return 0;
}
输出( ideone ):
A
当前,即使在主要的OS中,也无法在控制台中获得一致的Unicode输出.如果没有重大黑客攻击,通过cout
,wcout
,printf()
,wprintf()
等输出的简单Unicode文本将无法在Windows上运行.在Windows控制台中获取可读的Unicode文本的问题在于拥有并能够选择正确的Unicode字体. Windows的控制台在这方面非常不完善.请参见我的答案,并点击其中的链接.
Currently, it's impossible to get consistent Unicode output in the console even in major OSes. Simplistic Unicode text output via cout
, wcout
, printf()
, wprintf()
and the like won't work on Windows without major hacks. The problem of getting readable Unicode text in the Windows console is in having and being able to select proper Unicode fonts. Windows' console is quite broken in this respect. See this answer of mine and follow the link(s) in it.
这篇关于将char32_t打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!