Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unicode on Windows Console

35 views
Skip to first unread message

Michael Ludwig

unread,
Jan 7, 2012, 12:30:29 PM1/7/12
to perl-u...@perl.org
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

Michael Ludwig

unread,
Jan 11, 2012, 5:55:08 PM1/11/12
to perl-u...@perl.org
Michael Ludwig schrieb am 07.01.2012 um 18:30 (+0100):
> There's a WinAPI function that sets stdout to Unicode so you can
> read Cyrillic and Greek characters in the cmd.exe console window:

> Can I get the same feature in Perl?

Yes: https://metacpan.org/module/Win32::Unicode

Printing twelve hearts:

perl -MWin32::Unicode -lwe "printW qq( \x{2665}) x 12"

♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥

--
Michael Ludwig

Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯

unread,
Jan 12, 2012, 3:54:03 AM1/12/12
to perl-u...@perl.org
Run `chcp 65001`, see <http://enwp.org/chcp_(command)>. I have not
tested this.

Setting the encoding to Windows-1252 and then expecting to be able to
talk variations of UTF to it seems wrong.
signature.asc

Michael Ludwig

unread,
Jan 12, 2012, 2:50:33 PM1/12/12
to perl-u...@perl.org
Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯 schrieb am 12.01.2012 um 09:54 (+0100):
> Run `chcp 65001`, see <http://enwp.org/chcp_(command)>. I have not
> tested this.

Okay, that's just the regular chcp command.

> Setting the encoding to Windows-1252 and then expecting to be able to
> talk variations of UTF to it seems wrong.

Might seem wrong at first glance, I agree; but hey, this is Windows, and
it magically works, sort of bypassing the chcp setting! You just need
the C API call:

_setmode(_fileno(stdout), _O_WTEXT)

Or some equivalent of that, which is what Win32::Unicode seems to be
doing.
--
Michael Ludwig
0 new messages