develooper Front page | perl.unicode | Postings from January 2012

Unicode on Windows Console

Thread Next
From:
Michael Ludwig
Date:
January 7, 2012 09:31
Subject:
Unicode on Windows Console
Message ID:
20120107173029.GB952@Dago
There's a WinAPI function that sets stdout to Unicode so you can
read Cyrillic and Greek characters in the cmd.exe console window:

          \,,,/
          (o o)
------oOOo-(_)-oOOo------
// http://msdn.microsoft.com/en-us/library/tw4k6df8.aspx - _setmode
// crt_setmodeunicode.c
// This program uses _setmode to change
// stdout to Unicode. Cyrillic and Ideographic
// characters will appear on the console (if
// your console font supports those character sets).

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

int main(void) {
    _setmode(_fileno(stdout), _O_U16TEXT);
    wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
    return 0;
}
-------------------------

Compile it and will print "кошка 日本国", so a Russian name and three
fancy ideograms. (The ideograms aren't supported by my font, but that
is an unrelated problem; and I can't read them anyway; still they look
nice.) My console codepage is just 1252, Western European.

Can I get the same feature in Perl? I left the codepage at 1252 (can be
changes using CHCP) and tried the following:

* binmode STDOUT, ':encoding(UTF-16LE)'
* binmode STDOUT, ':encoding(UTF-16BE)'
* binmode STDOUT, ':encoding(UTF-8)'

None of these produced the desired effect. Any ideas?

I know I could use a Linux UTF-8 terminal or Cygwin/MinTTY, which is
what I'm using to write this mail, by the way; but the question is
specific to the Windows console in cmd.exe and how to make Perl use
its features.
-- 
Michael Ludwig

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About