从十六进制值打印 Unicode 的 Perl 程序
Perl Program to Print Unicode From Hex Value
我刚开始使用 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}
没有值打印
是的。你不能那样做。像这样的 \x 中间不允许进行变量插值。不过,您可以使用 chr() 来获取该字符。
is correct. For more info, you might want to read perluniintro.
从那里,您可以找到,例如:
At run-time you can use:
use charnames ();
my $hebrew_alef_from_name
= charnames::string_vianame("HEBREW LETTER ALEF");
my $hebrew_alef_from_code_point = charnames::string_vianame("U+05D0");
两者 chr($num)
and 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)';
我刚开始使用 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}
是的。你不能那样做。像这样的 \x 中间不允许进行变量插值。不过,您可以使用 chr() 来获取该字符。
从那里,您可以找到,例如:
At run-time you can use:
use charnames ();
my $hebrew_alef_from_name
= charnames::string_vianame("HEBREW LETTER ALEF");
my $hebrew_alef_from_code_point = charnames::string_vianame("U+05D0");
两者 chr($num)
and 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)';