There is a UTF-8 string with Russian text: "\xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82" .

You must output it to the console so that it works on the following three platforms and compilers:

  • Windows and VC ++,
  • Windows and MinGW,
  • Linux and gcc.

It is assumed that the console font allows you to show at least Russian letters.

It should be noted that the MinGW locales are not fully implemented, and many VC ++ solutions will not work.
MinGW can be taken here .

You can use #ifdef . You can use third-party libraries, such as Boost.


Here is an example of the MinGW solution of the day, but all three platforms need support .

 > type main.cpp #include <Windows.h> #include <string> #include <codecvt> #include <locale> int main() { std::wstring_convert<std::codecvt_utf8<wchar_t, 0x10ffff, std::little_endian>, wchar_t> conv; std::wstring ws = conv.from_bytes("[test \xd1\x82\xd0\xb5\xd1\x81\xd1\x82 \xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88]\n"); DWORD _; ::WriteConsoleW(::GetStdHandle(STD_OUTPUT_HANDLE), ws.c_str(), ws.size(), &_, nullptr); } > g++ -std=c++14 -O2 -Wall -pedantic main.cpp && a [test тест テスト] 
  • For MSVC, it seems to be a full-fledged working option - only translation to UTF-16. ru.stackoverflow.com/a/459299/10105 (but there is considered and input, and not only Russian text, but in general Unicode) - VladD
  • @VladD need cross-platform - Abyx
  • I doubt something about reachability. I couldn’t make friends with the utf-8 windsock console. Is that #ifdef _MSCVER + a special code for Windows. - VladD
  • But the goal is good, yes. - VladD
  • By the way, is there a solution for MinGW? It should also be friends with the Windows console. - VladD

1 answer 1

The Qt library (Core enough) is cross-platform and compiled by any compiler. Console output option:

 QString string = QString::fromUtf8("Привет мир !"); // для консоли Windows QTextCodec *codec = QTextCodec::codecForName("IBM 866"); // для Linux QTextCodec *codec = QTextCodec::codecForName("System"); QByteArray encodedString = codec->fromUnicode(string); std::cout << encodedString.data(); 

Qt codec list supported

  • one
    Have you checked that it works on all three platforms from the question? What if the console encoding is not CP866? - Abyx
  • And does this allow characters to be displayed in other languages? - VladD
  • I didn’t check, but your question was not once solved on the Internet for Windows and Qt. For Linux, it definitely works. The current console encoding can be retrieved from WinAPI ( GetConsoleCP ). I cannot say about all other possible languages. Another solution: stackoverflow.com/questions/4766301/… - paceholder