#!/usr/bin/env perl use strict; use warnings; my $str = "ab\x{cc}"; $str =~ s/(\P{PosixPrint})/sprintf("U+%04X", ord $1)/eg; print "$str\n";