zennehoy
zennehoy

Reputation: 6866

Printing UTF8 characters to linux console using C++

I am trying to get the unicode character macron (U+00AF), i.e., an overscore, to print consistently on various linux consoles. So far, some consoles work (e.g., putty ssh), others do not (e.g., ubuntu shell), and I have not been able to figure out what I am doing right in one case (probably luck) and wrong in the other.

I do know the basics of Unicode and Utf8, but I have not been able to figure out how to consistently get consoles to display the appropriate characters.

Any suggestions? Note that this is explicitly for unix consoles - all of the similar questions I have found focused on Windows-specific console commands.

Here is what I would effectively like to get working:

wchar_t post = L'¯'; //0xC2AF
std::wcout << post << std::endl;

Upvotes: 2

Views: 4185

Answers (2)

zennehoy
zennehoy

Reputation: 6866

Unfortunately nothing I tried or could find in the way of suggestions consistently displayed the appropriate character, so I ended up using an ASCII hyphen '-' as a close enough match.

Upvotes: 1

4pie0
4pie0

Reputation: 29754

The solution is to put it into stream as a multicharacter string:

std::string s = "\uC2AF";
std::cout << s << std::endl;

or to set a locale using

char* setlocale( int category, const char* locale);

function:

std::locale old_locale;  // current locale
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t w = 0xC2AF;
std::wcout << w << std::endl;
setlocale(LC_ALL, old_locale.name().c_str());  // restore locale

The final result is however dependent on many user settings (console, fonts, etc.), so there is no guarantee that it will be OK.

Upvotes: 0

Related Questions