如何使用 C++ 在 linux 终端中显示 unicode 字符? [英] How can I display unicode characters in a linux terminal using C++?

查看:41
本文介绍了如何使用 C++ 在 linux 终端中显示 unicode 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 linux 环境中使用 C++ 开发国际象棋游戏,我想在 bash 终端中使用 unicode 字符显示棋子.有没有办法使用 cout 显示符号?

I'm working on a chess game in C++ on a linux environment and I want to display the pieces using unicode characters in a bash terminal. Is there any way to display the symbols using cout?

输出一个骑士的例子会很好:♞ = U+265E.

An example that outputs a knight would be nice: ♞ = U+265E.

推荐答案

要输出 Unicode 字符,您只需使用输出流,就像输出 ASCII 字符一样.您可以将 Unicode 代码点存储为多字符串:

To output Unicode characters you just use output streams, the same way you would output ASCII characters. You can store the Unicode codepoint as a multi-character string:

 std::string str = "\u265E";
 std::cout << str << std::endl;

如果你想输出一个代码点在 ASCII 范围之上的单个 Unicode 字符,使用宽字符输出也可能很方便:

It may also be convenient to use wide character output if you want to output a single Unicode character with a codepoint above the ASCII range:

 setlocale(LC_ALL, "en_US.UTF-8");
 wchar_t codepoint = 0x265E;
 std::wcout << codepoint << std::endl;

但是,正如其他人所指出的,这是否正确显示取决于用户环境中的很多因素,例如用户终端是否支持Unicode显示,用户是否安装了正确的字体等. 对于大多数开箱即用的主流发行版(例如安装了 Gnome 的 Ubuntu/Debian)来说,这应该不是问题,但不要指望它可以在任何地方工作.

However, as others have noted, whether this displays correctly is dependent on a lot of factors in the user's environment, such as whether or not the user's terminal supports Unicode display, whether or not the user has the proper fonts installed, etc. This shouldn't be a problem for most out-of-the-box mainstream distros like Ubuntu/Debian with Gnome installed, but don't expect it to work everywhere.

这篇关于如何使用 C++ 在 linux 终端中显示 unicode 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆