本文介绍了在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!\n";
    std::cout << "Blah blah blah some gibberish unicode: ĐĄßĞĝ\n";
    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:


  • 启动 cmd / u 参数;

  • 调用 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输出到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:37