in reply to unicode/utf string to actual character

It should be "\x{6b63}" (only one "\")

Replies are listed 'Best First'.
Re^2: unicode/utf string to actual character
by xachen (Novice) on Sep 22, 2009 at 19:14 UTC
    This is where I struggle as when I tried this initially it didn't work and gave back nothing. Results:
    use Encode; $_ = "\u6b63"; $_ =~ s/\\u(.{4})/chr($1)/eg; $char = "\x{$_}"; print "raw is $char \n"; print "decode is " . Encode::decode("unicode", $char). "\n"; __FAILED OUTPUT__ raw is decode is
      • String literal "\u6b63" creates the string 6b63 (since \u uppercases the next character). To create the string \u6b63, you need to use string literal "\\u6b63".

      • You can't interpolate into the middle of an escape sequence (like "\x{$_}"). Interpolation and escapes occur at the same level.

      • You're passing the hex representation of a number to chr, but chr expects a number. You can use hex to do the conversion.

      • $_ doesn't contain the hex number of the character as you'd need in the escape sequence. It contains the decoded character already (as returned by chr).

      use open ':std', ':locale'; # So stuff you print shows up right. $_ = "\\u6b63"; print "orig string length is ", length($_), "\n"; print "orig string is $_\n"; s/\\u(.{4})/chr(hex($1))/eg; print "decoded string length is ", length($_), "\n"; print "decoded string is $_\n";
      orig string length is 6
      orig string is \u6b63
      decoded string length is 1
      decoded string is 正
      

      Update: Added lots