my $s = "téxt"; $s =~ s/([^\x20-\x7e])/sprintf '\u%04x', ord($1)/ge; print $s; # t\u00e9xt