简体   繁体   English

在 Windows 中使用 C++ 将 Unicode 输出到控制台

[英]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 的回答中所述:

  • Starting cmd with the /u argument;使用/u参数启动cmd
  • Calling chcp 65001 to change the output format;调用chcp 65001改变输出格式;
  • And setting a unicode font in the console (like Lucida Console Unicode).并在控制台中设置 unicode 字体(如 Lucida Console Unicode)。

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.hio.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,您必须做三件事:

  1. Start the command window with cmd /u .使用cmd /u启动命令窗口。 The /u says your programs will output Unicode. /u表示您的程序将输出 Unicode。
  2. Use chcp 65001 to indicate you want to use UTF-8 instead of one of the code pages.使用chcp 65001指示您要使用 UTF-8 而不是代码页之一。
  3. Select a font with more glyph coverage.选择具有更多字形覆盖的字体。 The command windows in newer versions of Windows offer Lucida Console Unicode .较新版本的 Windows 中的命令窗口提供Lucida Console Unicode My XP box has a subset of that called Lucida Console .我的 XP 盒子有一个叫做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.

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