Unicode字符Visual C ++ [英] Unicode character Visual C++

查看:219
本文介绍了Unicode字符Visual C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的程序使用unicode字符。
我在Windows 7 x32机器上使用Visual Studio 2010。

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

我想打印的是女王符号(\ul2655)它只是不工作。我已经设置我的解决方案使用unicode。

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

这是我的示例代码:

 #include <iostream>
 using namespace std;

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

    return 0;
 }

此外,我已经尝试了很多其他建议, (例如,更改cmd字体,应用chcp 65001,这与SetConsoleOutputCP(CP_UTF8)等相同)。

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).

有什么问题?这是我第一次遇到这样的情况。在Linux上,它不同。

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

谢谢。

推荐答案

我设法在控制台上打印棋子;这里有一些复杂的情况。

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

首先,你必须在stdout上启用UTF-16模式;这在此处以及这里,正如Mehrdad解释的那样。

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);

然后,即使输出正确到达控制台,您可能会收到垃圾,而不是预期字符;这是因为至少在我的机器(Windows 7)上,默认的控制台字体不支持棋子字形。

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.

为了解决这个问题,必须选择一种不同的TrueType字体来支持它们,但要使这种字体可用,您必须通过一些箍;

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;
}

在Windows和Linux上应该同样适用。

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:

这只能通过放大控制台字体来解决,使所有其他字符太大,不能使用。因此,总而言之,可能最好的解决方法只是编写一个GUI应用程序。

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.

这篇关于Unicode字符Visual C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆