本文介绍了使用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 directive here (include iostream)

using namespace std;

int main()
{

    cout << "Hello World!\n";
    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 ? b
$ b

What about 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).

这篇关于使用C ++将Unicode输出为控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:03