0

I'm trying to make my program work with unicode characters. I'm using Visual Studio 2010 on a Windows 7 x32 machine.

What I want to print is the queen symbol ("\ul2655") and it just doesn't work. I've set my solution to use unicode.

This is my sample code:

 #include <iostream>
 using namespace std;

 int main()
 {
    SetConsoleOutputCP(CP_UTF8);
    wcout << L"\u2655";

    return 0;
 }

Also, I've tried many other suggestions, but nothing worked. (eg. change the cmd font, apply chcp 65001, which is the same as SetConsoleOutputCP(CP_UTF8), etc).

What is the problem? It's for the first time I encounter such a situation. On linux, it's different.

Thank you.

Robert Lucian Chiriac
  • 686
  • 2
  • 15
  • 24
  • http://stackoverflow.com/questions/9230783/how-to-deal-with-the-unicode-characters-in-c – EToreo Oct 23 '13 at 17:42
  • 2
    Um, for one thing you set the console to UTF-8, but you're writing a UTF-16LE string. Second, you didn't tell `wcout` to use UTF-8. Third, the console doesn't support UTF-8. And fourth, this is a duplicate of [How do I print UTF-8 from c++ console application on Windows](http://stackoverflow.com/questions/1371012/how-do-i-print-utf-8-from-c-console-application-on-windows), assuming you really want UTF-8 and not just "any type of Unicode, I don't care what." – Raymond Chen Oct 23 '13 at 17:44
  • I just want to print my queen. Nothing else. I don't care how as long as it works. – Robert Lucian Chiriac Oct 23 '13 at 17:45

2 Answers2

5

Once I managed to print chess pieces on the console; there are several complexities involved here.

First of all, you have to enable UTF-16 mode on stdout; this is described here as well as here, and it's exactly as Mehrdad explained.

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

...

_setmode(_fileno(stdout), _O_U16TEXT);

Then, even if the output reached the console correctly, on the console you may get garbage instead of the intended characters; this comes form the fact that, at least on my machine (Windows 7), the default console font doesn't support the chess pieces glyphs.

To fix this, you have to choose a different TrueType font which supports them, but to make such a font available you have to go through some hoops; personally, I found out that DejaVu Sans Mono works just fine.

So, at this point, your code should work, and code like this (the example I wrote in the past to test this issue):

#include <wchar.h>
#include <stdio.h>
#include <locale.h>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif

enum ChessPiecesT
{
    King,
    Queen,
    Rock,
    Bishop,
    Knight,
    Pawn,
};

enum PlayerT
{
    White=0x2654,   /* white king */
    Black=0x265a,   /* black king */
};

/* Provides the character for the piece */
wchar_t PieceChar(enum PlayerT Player, enum ChessPiecesT Piece)
{
    return (wchar_t)(Player + Piece);
}

/* First row of the chessboard (black) */
enum ChessPiecesT TopRow[]={Rock, Knight, Bishop, Queen, King, Bishop, Knight, Rock};

void PrintTopRow(enum PlayerT Player)
{
    int i;
    for(i=0; i<8; i++)
        putwchar(PieceChar(Player, TopRow[Player==Black?i: (7-i)]));
    putwchar(L'\n');
}

/* Prints the eight pawns */
void PrintPawns(enum PlayerT Player)
{
    wchar_t pawnChar=PieceChar(Player, Pawn);
    int i;
    for(i=0; i<8; i++)
        putwchar(pawnChar);
    putwchar(L'\n');
}

int main()
{
#ifdef _WIN32
    _setmode(_fileno(stdout), _O_U16TEXT);
#else
    setlocale(LC_CTYPE, "");
#endif
    PrintTopRow(Black);
    PrintPawns(Black);
    fputws(L"\n\n\n\n", stdout);
    PrintPawns(White);
    PrintTopRow(White);
    return 0;
}

should work equally well on Windows and Linux.

Now you still have a problem: the glyphs will be too small to be meaningful in any way:

chessboard on console

this can be solved only by enlarging the console font, but you'd get all the other characters to be way too big to be usable. Thus, all in all, probably the best fix is just to write a GUI application.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

Try this instead

_setmode(_fileno(stdout), _O_U16TEXT);
user541686
  • 205,094
  • 128
  • 528
  • 886
  • It didn't work. It showed me the symbol with the 219 code from ASCII. – Robert Lucian Chiriac Oct 23 '13 at 17:44
  • @RobertEagle: It works for me, I see a crown. You have to do this at the very beginning of your program. What version of Windows are you on, and what version of VS are you using? Also did you remember to include `fcntl.h` and `stdio.h` and `io.h`? – user541686 Oct 23 '13 at 17:49
  • My VS version: 2010 10.0.30319.1 RTMRel My Windows version: Windows 7 Ultimate x32 SP1 I didn't forget anything. It just continues to show me that symbol, not the crown. – Robert Lucian Chiriac Oct 23 '13 at 17:55
  • @RobertEagle: what console font are you using? – Matteo Italia Oct 23 '13 at 17:58
  • @RobertEagle: Lucida Console doesn't support chess glyphs. You have to use a different character to get anything meaningful; check out the link in my answer. – Matteo Italia Oct 23 '13 at 18:03
  • @RobertEagle: Sorry for the confusion, I didn't mean I see a crown (er... queen) on the *console* itself. The character shows up as an unrenderable square, but when I copy-paste it somewhere else, it's a crown. My point was that it's not displaying an ASCII character, it really is Unicode -- and that fact doesn't depend on the font. – user541686 Oct 23 '13 at 18:07
  • @MatteoItalia: I think it's too much, since I have to make a presentation with it, on a different machine, again Windows. I can make another font available now, but I'm not that sure that I'll have the privilege to make the same changes on a different system in public. From what I hear, there are no quick fixes for this sort of things. Is it right? – Robert Lucian Chiriac Oct 23 '13 at 18:18
  • 2
    @RobertEagle: yep, there's no other easy fix AFAIK. That's why normally if you have to do anything beyond plain text you usually write a GUI application. – Matteo Italia Oct 23 '13 at 18:22
  • 1
    @MatteoItalia: If you run Console2 then you might be able to get around it, since I think it shows Unicode correctly. – user541686 Oct 23 '13 at 18:48