简体   繁体   English

如何使用g++将≠打印到终端?

[英]How to print out ≠ to terminal using g++?

I want to print in the terminal.我想在终端打印 I tried我试过

cout << '\u2248' << endl;
cout << '\U00002248' << endl;
cout << '≠' << endl;

which gives这使

14846344
14846344
14846368

I tried replacing the single quotes with double我尝试用双引号替换单引号

Ôëê
Ôëê
Ôëá

How can it be done?怎么做到呢? I'm curious what the explanation for the output I'm getting is?我很好奇我得到的输出的解释是什么? I'm running Netbeans 9 but have tested directly from command line with g++ too.我正在运行 Netbeans 9,但也直接从命令行使用 g++ 进行了测试。 I think this should be possible because echo ≠ produces the correct output in the Windows command prompt.我认为这应该是可能的,因为echo ≠在 Windows 命令提示符下产生正确的输出。

So, in C++, like in plain C, by default we can work just with ASCII characters.因此,在 C++ 中,就像在普通 C 中一样,默认情况下我们只能使用 ASCII 字符。 Char variables contains just 8 bits(1 byte) to store values so maximum - 2^8=256 different symbols can be coded by one char variable. Char变量仅包含 8 位(1 字节)来存储值,因此最大 - 2^8=256 个不同的符号可以由一个 char 变量编码。 Single quotes (like 'a') are storing char variables so inside of them can be placed just ASCII-character.单引号(如“a”)存储字符变量,因此它们内部可以只放置 ASCII 字符。 Your character is not a part of ASCII table and we need to change the encoding.您的字符不是 ASCII 表的一部分,我们需要更改编码。

For just print(not store/process) your character, you should use another encoding such as UTF-8.对于仅打印(而不是存储/处理)您的字符,您应该使用另一种编码,例如 UTF-8。 You can do it programmatically:您可以以编程方式执行此操作:

std::setlocale(LC_ALL, /*some system-specific locale name, probably */ "en_US.UTF-8");
std::cout << "\u2260" << std::endl;

Or via command line options to g++ (such as -finput-charset=UTF-16 ).或者通过 g++ 的命令行选项(例如-finput-charset=UTF-16 )。 As you can see, I'm using double quotes to print non-ASCII symbols to console.如您所见,我使用双引号将非 ASCII 符号打印到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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