[英]Output Unicode to console Using C++, in Windows
I'm still learning C++, so bear with me and my sloppy code.我还在学习 C++,所以请忍受我和我草率的代码。 The compiler I use is Dev C++.
我使用的编译器是 Dev C++。 I want to be able to output Unicode characters to the Console using cout.
我希望能够使用 cout 将 Unicode 字符输出到控制台。 Whenver i try things like:
每当我尝试以下操作时:
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
std::cout << "Blah blah blah some gibberish unicode: ĐĄßĞĝ\n";
system("PAUSE");
return 0;
}
It outputs strange characters to the console, like µA■Gg.它向控制台输出奇怪的字符,例如 µA■Gg。 Why does it do that, and how can I get to to display ĐĄßĞĝ?
为什么会这样,我如何才能显示 ĐĄßĞĝ? Or is this not possible with Windows?
或者这在 Windows 上是不可能的?
What about std::wcout
? std::wcout
呢?
#include <iostream>
int main() {
std::wcout << L"Hello World!" << std::endl;
return 0;
}
This is the standard wide-characters output stream.这是标准的宽字符输出流。
Still, as Adrian pointed out, this doesn't address the fact cmd
, by default, doesn't handle Unicode outputs.尽管如此,正如阿德里安指出的那样,这并没有解决
cmd
的事实,默认情况下,不处理 Unicode 输出。 This can be addressed by manually configuring the console, like described in Adrian's answer:这可以通过手动配置控制台来解决,如 Adrian 的回答中所述:
cmd
with the /u
argument;/u
参数启动cmd
;chcp 65001
to change the output format;chcp 65001
改变输出格式; You can also try to use _setmode(_fileno(stdout), _O_U16TEXT);
你也可以尝试使用
_setmode(_fileno(stdout), _O_U16TEXT);
, which require fcntl.h
and io.h
(as described in this answer , and documented in this blog post ). ,这需要
fcntl.h
和io.h
(如本答案所述,并记录在本博客文章中)。
I'm not sure Windows XP will fully support what you need.我不确定 Windows XP 是否会完全支持您所需要的。 There are three things you have to do to enable Unicode with a command console:
要使用命令控制台启用 Unicode,您必须做三件事:
cmd /u
.cmd /u
启动命令窗口。 The /u
says your programs will output Unicode. /u
表示您的程序将输出 Unicode。chcp 65001
to indicate you want to use UTF-8 instead of one of the code pages.chcp 65001
指示您要使用 UTF-8 而不是代码页之一。Lucida Console Unicode
.Lucida Console Unicode
。 My XP box has a subset of that called Lucida Console
.Lucida Console
的子集。 It doesn't have a very extensive repertoire, but it should be sufficient if you're just trying to display some accented characters.You can use the open-source {fmt} library to portably print Unicode text, including on Windows, for example:您可以使用开源 {fmt} 库来便携地打印 Unicode 文本,包括在 Windows 上,例如:
#include <fmt/core.h>
int main() {
fmt::print("Blah blah blah some gibberish unicode: ĐĄßĞĝ\n");
}
Output:输出:
Blah blah blah some gibberish unicode: ĐĄßĞĝ
This requires compiling with the /utf-8
compiler option in MSVC.这需要使用 MSVC 中的
/utf-8
编译器选项进行编译。
I don't recommend using wcout
because it is non-portable, for example:我不建议使用
wcout
因为它不可移植,例如:
std::wcout << L"Blah blah blah some gibberish unicode: ĐĄßĞĝ\n";
will print the ĐĄßĞĝ
part incorrectly on macOS or Linux ( https://godbolt.org/z/z81jbb ):将在 macOS 或 Linux ( https://godbolt.org/z/z81jbb ) 上错误地打印
ĐĄßĞĝ
部分:
Blah blah blah some gibberish unicode: ??ss??
and doesn't even work on Windows without changing the code page:并且在不更改代码页的情况下甚至不能在 Windows 上运行:
Blah blah blah some gibberish unicode:
Disclaimer : I'm the author of {fmt}.免责声明:我是 {fmt} 的作者。
You used the ANSI output stream.您使用了 ANSI 输出流。 You need to use
你需要使用
std::wcout << L"Blah blah blah some gibberish unicode: ĐĄßĞĝ\n";
Also, use std::cin.get()
, not system("PAUSE")
另外,使用
std::cin.get()
,而不是system("PAUSE")
In Linux, I can naively do:在 Linux 中,我可以天真地做:
std::cout << "ΐ , Α, Β, Γ, Δ, ,Θ , Λ, Ξ, ... ±, ... etc";
and it worked for most of the characters I tried.它适用于我尝试过的大多数角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.