I'm still learning C++, so bear with me and my sloppy code. The compiler I use is Dev C++. I want to be able to output Unicode characters to the Console using cout. Whenver i try things like:
#include <iostream> int main() { std::cout << "Hello World!\n"; std::cout << "Blah blah blah some gibberish unicode: ĐĄßĞĝ\n"; system("PAUSE"); return 0; } It outputs strange characters to the console, like µA■Gg. Why does it do that, and how can I get to to display ĐĄßĞĝ? Or is this not possible with Windows?
What about std::wcout ?
#include <iostream> int main() { std::wcout << L"Hello World!" << std::endl; return 0; } This is the standard wide-characters output stream.
Still, as Adrian pointed out, this doesn't address the fact cmd, by default, doesn't handle Unicode outputs. This can be addressed by manually configuring the console, like described in Adrian's answer:
cmd with the /u argument;chcp 65001 to change the output format;You can also try to use _setmode(_fileno(stdout), _O_U16TEXT);, which require fcntl.h and io.h (as described in this answer, and documented in this blog post).
You can use the open-source {fmt} library to portably print Unicode text, including on Windows, for example:
#include <fmt/core.h> int main() { fmt::print("Blah blah blah some gibberish unicode: ĐĄßĞĝ\n"); } Output:
Blah blah blah some gibberish unicode: ĐĄßĞĝ This requires compiling with the /utf-8 compiler option in MSVC.
I don't recommend using wcout because it is non-portable, for example:
std::wcout << L"Blah blah blah some gibberish unicode: ĐĄßĞĝ\n"; will print the ĐĄßĞĝ part incorrectly on macOS or Linux (https://godbolt.org/z/z81jbb):
Blah blah blah some gibberish unicode: ??ss?? and doesn't even work on Windows without changing the code page:
Blah blah blah some gibberish unicode: Disclaimer: I'm the author of {fmt}.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With