How can I display Unicode characters in linux terminal using C ++?

I am working on a chess game in C ++ in linux environment and I want to display fragments using Unicode characters in a bash terminal. Is there a way to display characters with cout?

An example that displays a knight will be nice: ♞ = U + 265E.

+6
c ++ terminal unicode
source share
2 answers

To output Unicode characters, you simply use the output streams, just like you should output ASCII characters. You can save the Unicode code as a multi-character string:

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

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

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

However, as others have noted, whether this is displayed correctly depends on many factors in the user environment, for example, whether the user terminal supports Unicode display, regardless of whether the user has the correct fonts installed, etc. This should not be a problem for most common distributions such as Ubuntu / Debian with Gnome installed, but do not expect it to work everywhere.

+5
source share

Sorry, I misunderstood your question. This code prints the white king in the terminal (tested it with KDE Konsole)

 #include <iostream> int main(int argc, char* argv[]) { std::cout <<"\xe2\x99\x94"<<std::endl; return 0; } 

Typically, encoding is specified in the locale. Try setting environment variables.

To tell applications UTF-8 Encoding, and assuming US English is your preferred language, you can use the following command:

 export LC_ALL=en_US.UTF-8 

Are you using a bare terminal or something under X-Server?

+3
source share

All Articles