Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Displaying unicode characters on the windows console

Reply
Thread Tools

Displaying unicode characters on the windows console

 
 
Shankar
Guest
Posts: n/a
 
      02-26-2008
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
 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      02-26-2008
On 26 Ún, 15:15, Shankar <(E-Mail Removed)> 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).
 
Reply With Quote
 
 
 
 
Shankar
Guest
Posts: n/a
 
      02-27-2008
On Feb 26, 7:33*pm, Ondra Holub <(E-Mail Removed)> wrote:
> On 26 Ún, 15:15, Shankar <(E-Mail Removed)> 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.
 
Reply With Quote
 
Eric.Malenfant@gmail.com
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 7:29 am, Shankar <(E-Mail Removed)> wrote:
> > On 26 Ún, 15:15, Shankar <(E-Mail Removed)> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      02-28-2008
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.)
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-28-2008
On Feb 28, 9:59 am, Juha Nieminen <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      02-28-2008
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.
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      03-01-2008
In article <ee266d9b-974a-4b0e-a74b-aa001d0d93a6
@t66g2000hsf.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
krassif krassif is offline
Junior Member
Join Date: Oct 2009
Posts: 1
 
      10-15-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python unicode utf-8 characters and MySQL unicode utf-8 characters Grzegorz ¦liwiñski Python 2 01-19-2011 07:31 AM
Problems displaying unicode-characters Jay Brahms Java 5 06-28-2006 05:46 PM
Posted Unicode Characters Displaying as "???" =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?= ASP .Net 3 05-08-2005 03:10 PM
Displaying Unicode characters Jonas ASP .Net 6 03-09-2005 12:59 PM
Unicode + jsp + mysql + tomcat = unicode still not displaying Robert Mark Bram Java 0 09-28-2003 05:37 AM



Advertisments