从Hex值打印Unicode的Perl程序

Que*_*ner 6 string unicode perl text-rendering

我开始使用Perl,并且对于如何在给定十六进制字符串变量的情况下呈现unicode字符感到困惑.

#!/usr/bin/perl
use warnings;

foreach my $i (0..10000) {
    my $hex = sprintf("%X", $i);
    print("unicode of $i is \x{$hex}\n");
}
print("\x{2620}\n");
print("\x{BEEF}\n");
Run Code Online (Sandbox Code Playgroud)

给我警告: Illegal hexadecimal digit '$' ignored at perl.pl line 9.

没有值打印 \x{$hex}

ike*_*ami 8

二者chr($num)pack('W', $num)产生由具有指定值的单个字符的字符串,就像"\x{XXXX}"一样.

因此,您可以使用

print("unicode of $i is ".chr(hex($hex))."\n");
Run Code Online (Sandbox Code Playgroud)

要不就

print("unicode of $i is ".chr($i)."\n");
Run Code Online (Sandbox Code Playgroud)

请注意,没有你的程序是没有意义的

use open ':std', ':encoding(UTF-8)';
Run Code Online (Sandbox Code Playgroud)