0

I am interested in displaying UTF-8 from a console application on Windows, without changing anything in the console from the user's side. Basically, what I want to do is display characters like "ა", but the output is not what I expect. The minimal code that shows this is below:

#include <iostream>
using namespace std;

int main() {
    cout << "ა" << endl;
}

I compile it with mingw-w64 with the command g++ -o filename filename.cpp.

Here is the output from the version command:

g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Following is the output after compilation:

сГР

Of course, this isn't what I want. I've seen some solutions online, for example:

How do I print UTF-8 from c++ console application on Windows
http://cplusplus.com/forum/windows/9797/
How to print UTF-8 characters on console using C

But none of them work. That first StackOverflow question talks about some fonts, but I want to do it automatically without any interaction from the user's side. The last one as well, however that is with a different compiler.

I know this is possible, as far as I know, at least python3 is written in C or C++, it supports UTF-8 by default, so I'm guessing technically that is possible. How, I have no idea.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Use `wcout << L"ა" << endl;`. And use strings like `std::wstring` and make sure you specify `L` in front of strings to specify that its a long (wide) char string (ie: `L"Hello World"`). – D-RAJ Jan 13 '21 at 15:35
  • Use `SetConsoleCP` with the code page for UTF-8 https://learn.microsoft.com/en-us/windows/console/setconsolecp – Richard Critten Jan 13 '21 at 15:35
  • @Dhiraj Wishal Thanks for reply, I have tried wcout << L"ა" << endl; but that doesn't work. No output at all, however compilation runs fine. – Nikoloz Tsiklauri Jan 13 '21 at 16:04
  • There are literally dozens/hundreds of questions on StackOverflow that deal with outputting Unicode strings to a Windows console. And you are saying that NONE of them work for you? I seriously doubt that. They work for other people. That being said, Windows doesn't really support UTF-8 in consoles, so use UTF-16 instead, like Dhiraj suggested. And there are UTF-16 based console output functions in the Win32 API. – Remy Lebeau Jan 13 '21 at 17:53
  • I have also tried cout << L"ა" << endl; and the output is 0x404002 . now I will try with SetConsoleCP – Nikoloz Tsiklauri Jan 13 '21 at 17:53
  • You can't use Unicode strings with `std::cout`, only with `std::wcout` – Remy Lebeau Jan 13 '21 at 17:54
  • well, no offence but yes there are a lots of resources, however internet is big and it's impossible to read everything. If I could find solution, why in the world would I need to create another duplicate question on the forum to make a clutter? Doesn't make any sense to me. Speaking about other questions on the forum, I have attached the links in the post and also said why they didn't work for me and I believe it's sufficient. Hence, if you know some resources to help me, you are more than welcome to send. I will of course look up the suggestions you sent above and thans for that. – Nikoloz Tsiklauri Jan 13 '21 at 18:11

0 Answers0