snaillu has asked for the wisdom of the Perl Monks concerning the following question:

I coded a short program, named unicode.pl. Below is the content.
use Encode; $vall = '\u805a\u5408\u6216\u8be6\u7ec6'; $vall =~ s/u(\w\w\w\w)/x{$1}/g; print $vall,"\n"; $str=Encode::encode("utf8", $vall); print $str;

The result is:

\x{805a}\x{5408}\x{6216}\x{8be6}\x{7ec6} \x{805a}\x{5408}\x{6216}\x{8be6}\x{7ec6}

In fact, I want to get the result like below:

\x{805a}\x{5408}\x{6216}\x{8be6}\x{7ec6} ¾ÛºÏ»òÏêϸ

To the sum, I want to get chinese charactor from unicode.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: How to to get chinese charactor from unicode?
by ikegami (Patriarch) on Mar 02, 2007 at 06:16 UTC
    You're trying to convert a Perl string literal to a string. To do that, use perl:
    $s = '\u805a\u5408\u6216\u8be6\u7ec6'; $s =~ s/u(\w\w\w\w)/x{$1}/g; $s = eval qq{"$s"}; print($s);

    Alternatively, you could interpret the original string yourself

    $s = '\u805a\u5408\u6216\u8be6\u7ec6'; $s =~ s/\\u([0-9A-Fa-f]{4})/chr(hex($1))/ge; print($s);

      I wasn't too happy with the second snippet. It had no way of escaping \. This is a fix:

      my $out = ''; for ($in) { /\G ([^\\]+) /xgc && ( $out .= $1 ); /\G \\u([0-9A-Fa-f]{4})/xgc && do { $out .= chr(hex($1)); redo; }; /\G \\(.) /xgc && do { $out .= $1; redo; }; }
Re: How to to get chinese charactor from unicode?
by zentara (Cardinal) on Mar 02, 2007 at 13:43 UTC