【问题标题】:Perl Program to Print Unicode From Hex ValuePerl 程序从十六进制值打印 Unicode
【发布时间】:2019-01-27 02:52:46
【问题描述】:

我开始使用 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} 没有打印值

【问题讨论】:

    标签: string perl unicode text-rendering


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2016-06-10
          • 2013-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多