Perl程序从十六进制值打印Unicode

时间:2018-08-20 23:46:19

标签: string perl unicode 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");

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

\x{$hex}不会显示任何值

3 个答案:

答案 0 :(得分:8)

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)';

答案 1 :(得分:4)

是的。你不能那样做。像这样\\中间不允许变量插值。您可以使用chr()来获取该字符。

答案 2 :(得分:1)

Randal's answer是正确的。有关更多信息,您可能需要阅读perluniintro

从那里,您可以找到例如:

  

在运行时,您可以使用:

 use charnames ();
 my $hebrew_alef_from_name
                      = charnames::string_vianame("HEBREW LETTER ALEF");
 my $hebrew_alef_from_code_point = charnames::string_vianame("U+05D0");
相关问题