Output Unicode to console Using C++, in Windows(在 Windows 中使用 C++ 将 Unicode 输出到控制台)
问题描述
我仍在学习 C++,所以请忍受我和我草率的代码.我使用的编译器是 Dev C++.我希望能够使用 cout 将 Unicode 字符输出到控制台.每当我尝试以下操作时:
I'm still learning C++, so bear with me and my sloppy code. The compiler I use is Dev C++. I want to be able to output Unicode characters to the Console using cout. Whenver i try things like:
#include <iostream>
int main()
{
std::cout << "Hello World!
";
std::cout << "Blah blah blah some gibberish unicode: ĐĄßĞĝ
";
system("PAUSE");
return 0;
}
它会向控制台输出奇怪的字符,例如 µA■Gg.为什么会这样,我如何才能显示 ĐĄßĞĝ?或者这在 Windows 上是不可能的?
It outputs strange characters to the console, like µA■Gg. Why does it do that, and how can I get to to display ĐĄßĞĝ? Or is this not possible with Windows?
推荐答案
std::wcout
怎么样?
#include <iostream>
int main() {
std::wcout << L"Hello World!" << std::endl;
return 0;
}
这是标准的宽字符输出流.
This is the standard wide-characters output stream.
不过,正如 Adrian 指出的那样,这并没有解决 cmd
的事实,默认情况下,它不处理 Unicode 输出.这可以通过手动配置控制台来解决,如 Adrian 的回答中所述:
Still, as Adrian pointed out, this doesn't address the fact cmd
, by default, doesn't handle Unicode outputs. This can be addressed by manually configuring the console, like described in Adrian's answer:
- 以
/u
参数开始cmd
; - 调用
chcp 65001
改变输出格式; - 并在控制台中设置 unicode 字体(如 Lucida Console Unicode).
- Starting
cmd
with the/u
argument; - Calling
chcp 65001
to change the output format; - And setting a unicode font in the console (like Lucida Console Unicode).
你也可以尝试使用_setmode(_fileno(stdout), _O_U16TEXT);
,这需要fcntl.h
和io.h
(如此答案中所述,以及记录在这篇博文).
You can also try to use _setmode(_fileno(stdout), _O_U16TEXT);
, which require fcntl.h
and io.h
(as described in this answer, and documented in this blog post).
这篇关于在 Windows 中使用 C++ 将 Unicode 输出到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Windows 中使用 C++ 将 Unicode 输出到控制台


- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01