Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 Perl,我得到了一个字符串:
agn\u00e8s b
我怎样才能将其转换为
agnès b
?
我试图使用下面的代码。
my $hex = "agn\u00e8s b"; $hex =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; print "$hex\n";
但它失败了,并显示了一些奇怪的字符。更重要的是,我还需要显示中文单词。例如,
\u9eb5\u5305
我如何将其转换为以下内容?
面包
my $s = "agn\\u00e8s b"; $s =~ s/\\u(....)/chr(hex($1))/eg; print "$s\n";
不要忘记对输出进行编码
use open ':std', ':encoding(UTF-8)';
用这个:
$hex =~ s/(\u[a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9])/encode(' utf-8', chr(hex($1)))/例如;