Search code examples
perlencodingdecodeencodeshift-jis

shift jis decoding/encoding in perl


When I try decode a shift-jis encoded string and encode it back, some of the characters get garbled: I have following code:

use Encode qw(decode encode);
$val=;
print "\nbefore decoding: $val";
my $ustr = Encode::decode("shiftjis",$val);
print "\nafter decoding: $ustr";
print "\nbefore encoding: $ustr";
$val = Encode::encode("shiftjis",$ustr);
print "\nafter encoding: $val";

when I use a string : helloソworld in input it gets properly decoded and encoded back,i.e. before decoding and after encoding prints in above code print the same value. But when I tried another string like : ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ

The end output got garbled.

Is it a perl library specific problem or it is a general shift jis mapping problem? Is there any solution for it?


Solution

  • You should simply replace the shiftjis with cp932.

    http://en.wikipedia.org/wiki/Code_page_932