How can I convert a Unicode codepoint (\uXXXX) into a character in Perl?

夙愿已清 提交于 2019-12-10 13:43:30

问题


I have some unicode codepoints (\u5315\u4e03\u58ec\u4e8c\u4e0a\u53b6\u4e4b), which I have to convert into actual characters they represent.

What's the simplest way to do so?


回答1:


Could Unicode::Escape be what you need?




回答2:


Sometimes I'd just use pack:

binmode STDOUT, ':utf8';

my $string = '\\u5315\\u4e03\\u58ec\\u4e8c\\u4e0a\\u53b6\\u4e4b';

$string =~ s/\\u(....)/ pack 'U*', hex($1) /eg;

print $string;



回答3:


use JSON::XS
print JSON::XS->new->decode('{"a":"\u5315\u4e03\u58ec\u4e8c\u4e0a\u53b6\u4e4b"}')->{a}



回答4:


perl -C -E'say"\x{5315}\x{4e03}\x{58ec}\x{4e8c}\x{4e0a}\x{53b6}\x{4e4b}"'

or funny way

perl -C -E'say map chr hex, qw(5315 4e03 58ec 4e8c 4e0a 53b6 4e4b)'


来源:https://stackoverflow.com/questions/2667599/how-can-i-convert-a-unicode-codepoint-uxxxx-into-a-character-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!