Displaying unicode characters on the windows console

Discussion in 'C++' started by Shankar, Feb 26, 2008.

  1. Shankar

    Shankar Guest

    Hi,

    I am not able to print the Unicode character on the console.

    I tried with these API's

    1) wprintf() displays ???????

    2) _cwprintf() displays [][][][][][]

    3) WriteConsoleW displays [][][][][][]



    Does anybody knows the solution? Please let me know.



    Thanks in advance
     
    Shankar, Feb 26, 2008
    #1
    1. Advertising

  2. Shankar

    Ondra Holub Guest

    On 26 Ún, 15:15, Shankar <> wrote:
    > Hi,
    >
    > I am not able to print the Unicode character on the console.
    >
    > I tried with these API's
    >
    > 1) wprintf() displays ???????
    >
    > 2) _cwprintf() displays [][][][][][]
    >
    > 3) WriteConsoleW displays [][][][][][]
    >
    > Does anybody knows the solution? Please let me know.
    >
    > Thanks in advance



    You have to set global locale. Try

    #include <locale>

    ...
    std::locale::global(std::locale(""));

    I cannot test it now, so it is possible, that it will not work (it
    should work for streams).
     
    Ondra Holub, Feb 26, 2008
    #2
    1. Advertising

  3. Shankar

    Shankar Guest

    On Feb 26, 7:33 pm, Ondra Holub <> wrote:
    > On 26 Ún, 15:15, Shankar <> wrote:
    >
    > > Hi,

    >
    > > I am not able to print the Unicode character on the console.

    >
    > > I tried with these API's

    >
    > > 1)  wprintf()               displays ???????

    >
    > > 2) _cwprintf()            displays [][][][][][]

    >
    > > 3) WriteConsoleW    displays [][][][][][]

    >
    > > Does anybody knows the solution? Please let me know.

    >
    > > Thanks in advance

    >
    > You have to set global locale. Try
    >
    > #include <locale>
    >
    > ...
    > std::locale::global(std::locale(""));
    >
    > I cannot test it now, so it is possible, that it will not work (it
    > should work for streams).


    I have tried with setlocale() and std::locale also but it doesnot
    work.

    Thanks.
     
    Shankar, Feb 27, 2008
    #3
  4. Shankar

    Guest

    On Feb 27, 7:29 am, Shankar <> wrote:
    > > On 26 Ún, 15:15, Shankar <> wrote:

    >
    > > > Hi,

    >
    > > > I am not able to print the Unicode character on the console.

    >
    > > > I tried with these API's

    >
    > > > 1) wprintf() displays ???????

    >
    > > > 2) _cwprintf() displays [][][][][][]

    >
    > > > 3) WriteConsoleW displays [][][][][][]

    >
    > > > Does anybody knows the solution? Please let me know.

    >
    >
    > I have tried with setlocale() and std::locale also but it doesnot
    > work.


    1) Does your console use a font that is able to display the
    characters? (For example, Lucida Console Unicode or someting like
    that.

    <windows-specific>
    2) I vaguely remember having to call something like SetConsoleCodePage
    to get unicode console output working on Windows.
    </windows-specific>

    HTH,

    Éric Malenfant
     
    , Feb 27, 2008
    #4
  5. Shankar wrote:
    > I am not able to print the Unicode character on the console.


    The first requirement is for the console to support unicode, which is
    often not the case. (I really don't know how well Windows' console
    supports unicode.)

    The second requirement is setting up the proper encoding. There are
    several ways to encode unicode characters, the most common one being
    UTF-8, although UTF-16 is sometimes used. What this means is that your
    program needs to print the text UTF-8-encoded, and the console needs to
    be able to decode them. This, naturally, requires support from both your
    program and the console software. If either one doesn't have the proper
    support, you are out of luck.

    Someone suggested setting up the proper locale from C++. I actually
    don't know if the locale library supports encoding in UTF-8. (I have to
    admit that I don't really know too much about the locale library to say
    anything about it.)
     
    Juha Nieminen, Feb 28, 2008
    #5
  6. Shankar

    James Kanze Guest

    On Feb 28, 9:59 am, Juha Nieminen <> wrote:
    > Shankar wrote:


    [...]
    > Someone suggested setting up the proper locale from C++. I
    > actually don't know if the locale library supports encoding in
    > UTF-8. (I have to admit that I don't really know too much
    > about the locale library to say anything about it.)


    It depends on what you mean by "support". If you imbue a locale
    which claims to support UTF-8, at the least, you should get
    automatic code translation to and from UTF-8 and the internal
    default wchar_t encoding when reading and writing using
    wfilebuf. For narrow chars, it's less obvious what the intent
    is: should the filebuf code translate into a default internal
    encoding (and if so, which), or not---presumably, the reason why
    all of the isxxx functions are locale dependent is to support
    different encodings, which would suggest not translating.

    Of course, most of the standard functions in std::ctype< char >
    are totally worthless for any multibyte encoding.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 28, 2008
    #6
  7. Shankar wrote:
    > Hi,
    >
    > I am not able to print the Unicode character on the console.
    >
    > I tried with these API's
    >
    > 1) wprintf() displays ???????
    >
    > 2) _cwprintf() displays [][][][][][]
    >
    > 3) WriteConsoleW displays [][][][][][]
    >
    >
    >
    > Does anybody knows the solution? Please let me know.



    I have tried myself, and it isn't possible at least under Windows XP and
    Visual C++ Express 2008.

    Even using a console font that enables you to write in a non-english
    language (or at least what you type appears on the console), when
    outputing, it comes corrupted with ?s etc. I suppose it is a deficiency
    of the Windows console (that is, a deficiency of Windows).

    I have tried by both running command.com and cmd.exe to open a console.
     
    Ioannis Vranos, Feb 28, 2008
    #7
  8. Shankar

    Jerry Coffin Guest

    In article <ee266d9b-974a-4b0e-a74b-aa001d0d93a6
    @t66g2000hsf.googlegroups.com>, says...
    > Hi,
    >
    > I am not able to print the Unicode character on the console.
    >
    > I tried with these API's
    >
    > 1) wprintf() displays ???????
    >
    > 2) _cwprintf() displays [][][][][][]
    >
    > 3) WriteConsoleW displays [][][][][][]


    WriteConsoleW can display any characters present in the font your
    console is using. A Windows font includes a default glyph that's
    displayed when you ask to display a character for which the font doesn't
    include a glyph -- and it looks like that's what you're getting here.

    Consider the following code:

    #include <windows.h>

    int main() {

    const int num = 256;
    const int start = 0x100;

    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

    wchar_t buffer[num];

    for (wchar_t val = start; val<start+num; val++)
    buffer[val-start] = val;

    DWORD ignore;
    WriteConsoleW(console, buffer, num, &ignore, NULL);

    return 0;
    }

    When I run this on a console set to use the Lucida Console font, some
    characters show proper glyphs and others show the default glyph (the
    empty square box).

    To get your code to work correctly, you'll need to ensure use of a font
    that includes glyphs for the characters you're using. Unfortunately,
    Windows has only minimal support for selecting the font to be used by a
    particular console. It includes GetCurrentConsoleFont to retrieve
    information about the font, but does NOT include a SetConsoleFont or
    anything like that to change the font -- i.e. from the viewpoint of your
    program, the font is read-only. Even at best, the selection of fonts for
    a console is generally very limited.

    Ultimately, however, this is really a question about system
    administration rather than programming -- from a programming viewpoint,
    WriteConsoleW (for one example) is doing what you've asked. You see the
    glyph for the character you've specified; from there it's a matter of
    selecting a font that includes suitable glyphs for the characters you're
    using.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Mar 1, 2008
    #8
  9. Shankar

    krassif

    Joined:
    Oct 15, 2009
    Messages:
    1
    It is an old post, but had the same problem earlier today.

    The answer was found in the .NET Console::OutputEncoding setter. Internally it calls SetConsoleOutputCP(uint codePage) (in kernel32) .

    For UTF8 the code page is UTF7_CODEPAGE = 0xfde8; for Unicode 0x4b0; big endian Unicode 0x4b1;

    Cheers,
    -Krassimir
     
    krassif, Oct 15, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonas

    Displaying Unicode characters

    Jonas, Mar 8, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    3,187
    Juan T. Llibre
    Mar 9, 2005
  2. =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=

    Posted Unicode Characters Displaying as "???"

    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=, May 8, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    609
    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=
    May 8, 2005
  3. Jay Brahms
    Replies:
    5
    Views:
    1,860
    Anthony Simuel
    Jun 28, 2006
  4. Grzegorz ¦liwiñski
    Replies:
    2
    Views:
    1,024
    Grzegorz ¦liwiñski
    Jan 19, 2011
  5. Alf P. Steinbach
    Replies:
    11
    Views:
    2,303
    Alf P. Steinbach
    Nov 23, 2011
Loading...

Share This Page