Escape multibyte characters

  • A+

With Python - I can take a string and return it with multibyte characters UTF-8 escaped:

$ python3 -c 'print("hello ☺ world".encode("utf-8"))' b'hello /xe2/x98/xba world' 

Or unicode escaped:

$ python3 -c 'print("hello ☺ world".encode("unicode-escape"))' b'hello //u263a world' 

Can Perl do something like this? I tried "quotemeta" but it seems it is not the right tool:

$ perl -e 'print quotemeta("hello ☺ world/n");' hello/ /�/�/�/ world/ 


Data::Dumper, for one, can do this.

use utf8; use Encode; use Data::Dumper; $Data::Dumper::Terse = 1;   # suppress  '$VAR1 = ...' header $Data::Dumper::Useqq = 1;   # make output printable  print Dumper("hello ☺ world"); print Dumper(encode("UTF-8","hello ☺ world")); 


"hello /x{263a} world" "hello /342/230/272 world" 

Update: the relevant function in the Data::Dumper module is qquote, so you can skip setting $Useqq and $Terse:

use utf8; use Encode; use Data::Dumper;  print Data::Dumper::qquote("hello ☺ world"), "/n"; print Data::Dumper::qquote(encode("UTF-8","hello ☺ world")), "/n"; 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: