perl -Mutf8 -Mstrict -Mwarnings -E ' my $x = "\x966\x959\x959\x946\x945\x961"; # Greek "foobar" $x =~ s/([[:^ascii:]])/sprintf("%%U%x", unpack q{U}, $1)/eg; say $x; '