简体   繁体   中英

perl pack a utf-8 Chinese Character how to unpack to get this Character?

I am learning pack function in perl. I found I cannot unpack and get the origin value. Following is the code. File encode utf8. How can I unpack and get the Chinese character.

I have checked the perldoc . I am not sure which TEMPLATE I can use. Document said that:

UA Unicode character number. Encodes to a character in character mode and UTF-8 (or UTF-EBCDIC in EBCDIC platforms) in byte mode.

So I tried U . But it didn't work.

use Encode;

open(DAT,"+>T.dat");
binmode(DAT,":raw");

print DAT pack("f",-3.938345);
print DAT pack("l",1234556);
print DAT pack("U*","我");

seek(DAT,0,0);
read(DAT,$Val,4);
$V=unpack("f",$Val);
print "V $V\n";
read(DAT,$int,4);
$I=unpack("l",$int);
print "int $I\n";
read(DAT,$HZ,4);
$HZ=unpack("U*",$HZ);
print("HZ $HZ\n");

close(DAT);

And I have another question, I know one Chinese Character only take 2 bytes if encoded in GB2312. How can I pack one Character and only take 2 bytes space?

Unicode pack and unpack in Perl works the other way round:

use utf8;
binmode STDOUT,":utf8";

my $packed = pack("U*", 0x6211);
print "$packed\n";  # 我

my $unpacked = unpack("U*", "我");
printf "0x%X\n", $unpacked;  # 0x6211

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM