Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 855
Peterim Avatar asked Apr 19 '10 13:04

Peterim


1 Answers

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;
like image 70
brian d foy Avatar answered Sep 24 '22 13:09

brian d foy