首页 > 解决方案 > Perl 程序从十六进制值打印 Unicode

问题描述

我从 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");

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

并且没有打印价值\x{$hex}

标签: stringperlunicodetext-rendering

解决方案


两者都chr($num)生成pack('W', $num)一个由具有指定值的单个字符组成的字符串,就像这样"\x{XXXX}"做一样。

因此,您可以使用

print("unicode of $i is ".chr(hex($hex))."\n");

要不就

print("unicode of $i is ".chr($i)."\n");

请注意,您的程序没有任何意义

use open ':std', ':encoding(UTF-8)';

推荐阅读