3

当我尝试解码 shift-jis 编码的字符串并将其编码回来时,一些字符会出现乱码:我有以下代码:

使用编码 qw(解码编码);
$val=;
print "\n解码前:$val";
我的 $ustr = Encode::decode("shiftjis",$val);
print "\n解码后:$ustr";
print "\n编码前:$ustr";
$val = 编码::encode("shiftjis",$ustr);
print "\n 编码后:$val";

当我在输入中使用字符串 : helloソworld 时,它会被正确解码和编码回来,即在解码之前和编码之后在上面的代码中打印打印相同的值。但是当我尝试另一个字符串时: ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ

最终输出出现乱码。

它是 perl 库特定的问题还是一般的 shift jis 映射问题?有什么解决办法吗?

4

2 回答 2

3

您应该简单地将 替换shiftjiscp932

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

于 2011-04-02T12:42:34.057 回答
2

你缺乏错误检查。

use utf8;
use Devel::Peek qw(Dump);
use Encode qw(encode);

sub as_shiftjis {
    my ($string) = @_;
    return encode(
        'Shift_JIS',    # http://www.iana.org/assignments/character-sets
        $string,
        Encode::FB_CROAK
    );
}

Dump as_shiftjis 'helloソworld';
Dump as_shiftjis 'ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ';

输出:

SV = PV(0x9148a0) at 0x9dd490
  REFCNT = 1
  FLAGS = (TEMP,POK,pPOK)
  PV = 0x930e80 "hello\203\\world"\0
  CUR = 12
  LEN = 16
"\x{2160}" does not map to shiftjis at …
于 2011-04-02T08:16:40.360 回答