Displaying unicode characters on the windows console

S

Shankar

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
 
O

Ondra Holub

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

Shankar

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

Eric.Malenfant

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
 
J

Juha Nieminen

Shankar said:
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.)
 
J

James Kanze

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

Ioannis Vranos

Shankar said:
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.
 
J

Jerry Coffin

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.
 
Joined
Oct 15, 2009
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top