Preferred way to decode characters (to unicode) read from console in Windows

82 views Asked by At

When reading from the console in Windows, the encoding of input string depends on the code page of the console. What is the best way to convert the input string to unicode? I have created two examples, the first reads the input encoding of the console,

use feature qw(say);
use strict;
use warnings;
use Encode qw(decode);
use Win32;
my $cp = Win32::GetConsoleCP();  # get the input code page
chomp(my $str = <STDIN>);
printf "%vx\n", $str;
my $str_unicode = decode("cp" . $cp, $str);
printf "%vx\n", $str_unicode;

the second sets the input encoding of the console:

use feature qw(say);
use strict;
use warnings;
use Encode qw(decode_utf8);
use Win32;    
Win32::SetConsoleCP(65001);  # set the input code page
chomp(my $str = <STDIN>);
printf "%vx\n", $str;
my $str_unicode = decode_utf8($str);
printf "%vx\n", $str_unicode;

What is the best practice, the first or the second or something else?

0

There are 0 answers