I\'m trying to write unicode strings to the screen in C++ on Windows. I changed my console font to Lucida Console and I set the output to CP_UTF8 a
Although you've set your console to expect UTF-8 output, I suspect that your compiler is treating string literals as being in some other character set. I don't know why the C compiler acts differently.
The good news is that C++11 includes some support for UTF-8, and that Microsoft has implemented the relevant portions of the Standard. The code is a little hairy, but you'll want to look into std::wstring_convert (converts to and from UTF-8) and the
You can use those functions to convert to UTF-8, and assuming your console is expecting UTF-8, things should work correctly.
Personally, when I need to debug something like this, I often direct the output to a text file. Text editors seem to handle Unicode better than the Windows console. In my case, I often output the code points correctly, but have the console set up incorrectly so that I still end up printing garbage.
I can tell you that this worked for me in both Linux (using Clang) and Windows (using GCC 4.7.3 and Clang 3.5; you need to add "std=c++11" to the command line to compile with GCC or Clang):
#include
int main()
{
const char text[] = u8"Россия";
std::printf("%s\n", text);
}
Using Visual C++ (2012, but I believe it would also work with 2010), I had to use:
#include
#include
#include
#include
int main()
{
std::wstring_convert> converter;
auto text = converter.to_bytes(L"Россия");
std::printf("%s\n", text.c_str());
}