Reputation: 501
I am using Perl, and I got a string:
agn\u00e8s b
How can I convert it to
agnès b
?
I have tried to use the below code.
my $hex = "agn\u00e8s b";
$hex =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
print "$hex\n";
But it failed and displayed some strange character. What is more, I need to display Chinese words as well. For example,
\u9eb5\u5305
How do I it convert to the following?
麵包
Upvotes: 2
Views: 5773
Reputation: 385789
my $s = "agn\\u00e8s b";
$s =~ s/\\u(....)/chr(hex($1))/eg;
print "$s\n";
Don't forget to encode your output
use open ':std', ':encoding(UTF-8)';
Upvotes: 3
Reputation: 2419
Use this:
$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)))/eg;
Upvotes: 0