unpack()を使わなくても、次のような感じで文字コードを参照できるみたい。
※他にも方法はあるようですので、「perl 文字参照」でググりましょう
#!/usr/local/bin/perl use strict; use utf8; use Encode qw/encode decode :fallbacks/; main(); sub main { for my $char (qw/㈱ 郄 粼/){ #株 高 崎 my $c_enc_10 = Encode::encode('ascii', $char, FB_HTMLCREF); #10進 my $c_enc_16 = Encode::encode('ascii', $char, FB_XMLCREF); #16進 print encode('utf8', "$char -Encode-> 10進)$c_enc_10 16進)$c_enc_16"),"\n"; my $c_ord_10 = '&#'. ord($char) .';'; #10進 my $c_ord_16 = '&#x'. sprintf("%x", ord($char)) .';'; #16進 #%Xでは、16進数が大文字表記されます。 print encode('utf8', "$char -ord----> 10進)$c_ord_10 16進)$c_ord_16"),"\n"; } }
実行結果
$ ./foo.pl ㈱ -Encode-> 10進)㈱ 16進)㈱ ㈱ -ord----> 10進)㈱ 16進)㈱ 郄 -Encode-> 10進)髙 16進)髙 郄 -ord----> 10進)髙 16進)髙 粼 -Encode-> 10進)﨑 16進)﨑 粼 -ord----> 10進)﨑 16進)﨑
文字参照の値を文字に変換するには chr()
$ORG_STR =~ s/\&\#x([0-9a-f]{4})/chr(hex($1))/egoi; print "$ORG_STR\n";
その他参考
- http://e8y.net/mag/015-encode/
- javascriptでは escape() / unescape()