can't print unicode characters

I can't print the chess symbols on the console (♔♕♖...)

I've already tried
1
2
3
4
5
  HANDLE cons = GetStdHandle(STD_OUTPUT_HANDLE);
  wchar_t p[] = L"Queen: \u265B.\n";
  // wprintf(p);
  DWORD n;
  WriteConsoleW(cons, p, wcslen(p), &n, NULL );


and
1
2
  setlocale(LC_CTYPE, "");
  wprintf(L"\u2656");


and
1
2
3
  SetConsoleOutputCP(65001);
  char q[] = "King: \xE2\x99\x94.\n";
  printf(q);


all 3 didn't work

help!
Last edited on
I really doubt cmd has logic/fonts to handle emojis. I suppose you could do some hackery with system fonts to get it to work but my guess is it wouldn't be easy, especially since full unicode support is more than just fonts.

ConEmu has better unicode support: https://conemu.github.io/

Are you on Windows 7 or Windows 10? The latest versions of Windows 10 have a new app called the "Windows Terminal", and it has support for unicode/emojis (so I've heard).

Last edited on
There are two things you must do to print them:

  1. Set the output code page to UTF-8 (65001) [which you have done]
  2. Select a proper font: DejaVu Sans Mono

Unfortunately, there is a problem with part 2: many Windows 10 users still do not have the Windows fix for SetCurrentConsoleFontEx() to be able to use that font, as its name is 16 characters long, which is two too many for the old, broken version (max 15 characters, including the null).

Microsoft claims that all current Windows 10 users should have had that fixed by now, but my Windows 10 does not.

What you need to do, then, is prepare an install script to create a .lnk file that starts your program, and in that select the correct font.

To make the effort seamless, your program can verify that the font is correct or not at startup. If not, attempt to set it with SetCurrentConsoleFontEx(). Test again, and if not correct, create the .lnk file and launch it (with system() or CreateProcess() or ShellExecuteEx() or whatever works for you).

.Lnk files only work with absolute paths, so you cannot simply copy one to your end user. They are pretty simple to create and modify, though.


Also, since you are playing with the user's console, it is a really good idea not to do anything that will make your user hate you. Make sure you start with a title that the system can identify a console profile and reuse, instead of screwing around with all consoles. The best way to do this, IMO, is to simply use the .lnk and system()/whatever commands to relaunch properly, and Windows will make things right.

This has the good side effect of being able to resize the console to what you want and disable user resize a viable option.


I don’t know how soon I can get a working example to you, but if you give me a day or two I’ll post something that makes a nice chessboard that works with both Windows and Linux.

Hope this helps.
Interesting I've never heard of that.
Does what you say apply to only Win10, or can it be used for basic Unicode characters in previous versions?
I do not know if the limitation existed in previous versions. Setting the font via a .lnk is the best way, IMHO. (Previous versions were not so friendly when playing with the console’s font anyway, and I have only recently played with the Console API’s relatively new font functions.)

The font issue has always been there on Windows, and even though Lucida Unicode has traditionally been a good choice for the console font, it does not actually provide much better than Courier New or Consolas. Aaand the console will not do any magic substitutions with other fonts to provide a Unicode character glyph.


Also, this presupposes that your system has that font installed, of course. I am pretty sure it comes as part of Windows 10.
Topic archived. No new replies allowed.