gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:30:28 25 4
gpt4 key购买 nike

我从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}的值打印

最佳答案

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

关于string - Perl程序从十六进制值打印Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51939835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com