且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 C++ 中打印 Unicode 字符?

更新时间:2022-11-08 23:10:02

要表示字符,您可以使用通用字符名称 (UCN).字符 'ф' 的 Unicode 值是 U+0444,所以在 C++ 中你可以把它写成 'u0444' 或 'U00000444'.此外,如果源代码编码支持此字符,那么您可以直接在源代码中写入它.

To represent the character you can use Universal Character Names (UCNs). The character 'ф' has the Unicode value U+0444 and so in C++ you could write it 'u0444' or 'U00000444'. Also if the source code encoding supports this character then you can just write it literally in your source code.

// both of these assume that the character can be represented with
// a single char in the execution encoding
char b = 'u0444';
char a = 'ф'; // this line additionally assumes that the source character encoding supports this character

打印出这些字符取决于您要打印的内容.如果您打印到 Unix 终端模拟器,终端模拟器正在使用支持此字符的编码,并且该编码与编译器的执行编码匹配,那么您可以执行以下操作:

Printing such characters out depends on what you're printing to. If you're printing to a Unix terminal emulator, the terminal emulator is using an encoding that supports this character, and that encoding matches the compiler's execution encoding, then you can do the following:

#include <iostream>

int main() {
    std::cout << "Hello, ф or u0444!
";
}

这个程序要求'ф'可以用单个字符表示.在 OS X 和大多数现代 Linux 安装上,这都可以正常工作,因为源代码、执行和控制台编码都将是 UTF-8(支持所有 Unicode 字符).

This program does not require that 'ф' can be represented in a single char. On OS X and most any modern Linux install this will work just fine, because the source, execution, and console encodings will all be UTF-8 (which supports all Unicode characters).

Windows 的事情更难,而且有不同的可能性和不同的权衡.

Things are harder with Windows and there are different possibilities with different tradeoffs.

如果你不需要可移植的代码(你将使用 wchar_t,这应该在其他所有平台上真正避​​免),***的方法是将输出文件句柄的模式设置为仅采用 UTF-16数据.

Probably the best, if you don't need portable code (you'll be using wchar_t, which should really be avoided on every other platform), is to set the mode of the output file handle to take only UTF-16 data.

#include <iostream>
#include <io.h>
#include <fcntl.h>

int main() {
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"Hello, u0444!
";
}

可移植代码更难.