0

I am a beginner programmer and I'm working on a simple game. I want to output Unicode symbols like Alt+219 █ to the console. I am using CodeBlocks on Windows 10 and I am using GNU GCC compiler. Here is what I'm trying to do:

#include <iostream>

using namespace std;

int main()
{
    cout << "\u2588";
    return 0;
}

What I get on console is a few strange symbols. This happens even with other Unicode characters, but the symbols are different.

I tried using wcout, but then i get just a blank space instead.

I also tried things like this:

_setmode(_fileno(stdout), _O_U16TEXT);

But then I get errors that _fileno and _O_U16TEXT was not declared, though I imported the libraries that seem to be necessary for this:

#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <fstream>

The Unicode symbols are not totally necessary for my game, but I just want it look nicer.

dalle
  • 18,057
  • 5
  • 57
  • 81
  • 1
    Not a duplicate, but have a read of http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how – Richard Critten Jul 24 '16 at 12:03
  • 1
    Possible duplicate of [Output unicode strings in Windows console app](http://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app) – roeland Jul 24 '16 at 21:59

3 Answers3

0

That's because you're trying to print wchar_t (16 bits long) characters with std::cout that only supports char (8 bits long). Use std::wcout instead of std::cout, wcout is meant for wide characters.

  • I tried this with wcout, but nothing gets outputted, unless i go to \u256 or lower. When i try \u257 or higher it outputs either nothing or multiple symbols. – Urban Rock Jul 24 '16 at 15:03
0

The Windows Command Prompt uses CP850 as encoding by default, and won't be able to correctly display this symbol or any Unicode symbol for that matter unless you change it.

It has, however, its own version of it, \xDB that is probably what you are looking for.

For other symbols you can use in the Windows Command Prompt, open charmap.exe and check the font Terminal, there's a range of useful symbols that should be compatible with CP850 and display under the Command Prompt the same way they are presented there.

Community
  • 1
  • 1
Havenard
  • 27,022
  • 5
  • 36
  • 62
  • The Windows Command Prompt uses an ANSI encoding by default, which varies by Windows localization. CP850 is a Western European default. It is CP437 in the US. The `chcp` command will show the current code page. – Mark Tolonen Jul 24 '16 at 18:32
  • And another thing: how to know the char code that i have to use? The charmap still shows the code of Full Block █ as U+2588, though i need '\xDB' for that to work – Urban Rock Jul 24 '16 at 19:28
  • @UrbanRock If the font is Terminal it should show 0xDB for that symbol, at least it does for me. – Havenard Jul 24 '16 at 20:01
0

Other answers have addressed treating the string as a UTF-16 literal. You can also tell the compiler to treat it as a utf-8 literal (forcing it to encode all characters within it as utf-8) by prefixing it with u8:

#include <iostream>

using namespace std;

int main()
{
    cout << u8"\u2588";
    return 0;
}

Note that windows consoles aren't by default set to treat output as utf-8, so this still won't display correctly without some additional settings.

Cubic
  • 14,902
  • 5
  • 47
  • 92