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

Unicode on Windows Console

Thread Next
Michael Ludwig
January 7, 2012 09:31
Unicode on Windows Console
Message ID:
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)
// - _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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About