How to convert the U+XXXXX sequence to character in perl?

时间:2015-06-26 10:18:17

标签: perl

This

perl -CO -E 'say "\N{U+00E1}"'

prints

á

How to achieve to get the same á with the following:

echo "U+00E1" | perl -CO -lnE 'say what_here($_)'

4 个答案:

答案 0 :(得分:4)

Use charnames:

echo "U+00E1" | perl -Mcharnames=short -CO -lnE 'say charnames::vianame($_)'

You can also use eval, if you can control what comes in as input:

echo "U+00E1" | perl -CO -lnE 'say eval qq("\\N{$_}")'

答案 1 :(得分:2)

Pure basic Perl解决方案:

echo "U+00E1" | perl -C -nE 'say chr(hex) if s/U\+//'

请不要使用不安全的(eval)解决方案,如:

echo "U+00E1" | perl -C -nE 's/U\+/0x/;eval qq{say chr($_)}'

答案 2 :(得分:1)

使用pack

echo U+0041 U+0042 U+0043 U+00E1 | perl -ne"print pack 'U', hex for /\p{hex}+/g"

输出

ABCá

答案 3 :(得分:0)

echo "U+00E1" | perl -CO -lnE 'eval "\$a=\"\\N{$_}\";"; say $a;'