http://www.perlmonks.org?node_id=989002


in reply to Regex to convert non-printable to printable char

Just a note, \x7F is also unprintable character, In one of my scripts, I use following translation to filter non-printable characters.

tr[\0-\x1F\x7F] [\x{2400}-\x{241F}\x{2421}]

\x{2420} is space, in case you have noticed a gap in range. If you don't want to replace them with Unicode non-printable graphics characters, you could replace \x{2400}-\x{241F}\x{2421} with ? or \x{FFFD} (Unicode replacement character).

Also, in most cases you wouldn't want to match \x09 (tab), \x0A (line feed) or \x0D (carriage return). You could use this translation when you don't want to match them.

tr[\0-\x08\x0B\x0C\x0E-\x1F\x7F] [\x{2400}-\x{2408}\x{240B}\x{240C}\x{240E}-\x{241F}\x{2421}]

It also doesn't work on EBCDIC, if you want to match EBCDIC, most likely you will need different range of characters (\x00-\x3F\xFF).