Reputation: 11
How can I convert a utf7 string to iso-8859-1 format? I tried the following but it prints the wrong result:
use Encode qw(encode decode);
$data ='t+AOQ-m+AOQ- on mit+AOQ- on';
$data = encode("iso-8859-1", decode("utf7", $data));
print $data; #result Tämä on mitä on
This prints tΣmΣ on mitΣ on
, but it should print Tämä on mitä on
Upvotes: 1
Views: 201
Reputation: 386501
use Encode qw(encode decode);
$data ='t+AOQ-m+AOQ- on mit+AOQ- on';
$data = encode("iso-latin-1", decode("utf7", $data));
printf "%v02X\n", $data;
gives
74.E4.6D.E4.20.6F.6E.20.6D.69.74.E4.20.6F.6E
which is the iso-latin-1 encoding of
tämä on mitä on
If you see something other than "tämä on mitä on", it's because the tool you are using to view it is treating it as something other than iso-latin-1.
Do you want iso-latin-1, or do you want it to show correctly in your tool? If it's the latter, use the correct encoding instead of iso-latin-1.
If command-line tool chcp
returns 437 (for example), you want cp437.
use Encode qw(encode decode);
my $raw ='t+AOQ-m+AOQ- on mit+AOQ- on';
my $data = decode("utf7", $raw);
print encode("cp437", "$data\n");
or better yet
use open ':std', ':encoding(cp437)';
use Encode qw(decode);
my $raw ='t+AOQ-m+AOQ- on mit+AOQ- on';
my $data = decode("utf7", $raw);
print "$data\n";
Upvotes: 5