Skip to content

Instantly share code, notes, and snippets.

@roycewilliams
Created July 6, 2019 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roycewilliams/687764702943eeef68cf62ae0a21aeae to your computer and use it in GitHub Desktop.
Save roycewilliams/687764702943eeef68cf62ae0a21aeae to your computer and use it in GitHub Desktop.
HEX-ify plains that need it
#!/usr/bin/env perl
#-----------------------------------------------------------------------
# Created: 2017-11-21
# $Id: hexify,v 1.2 2017/11/22 06:29:35 root Exp root $
#-----------------------------------------------------------------------
# FIXME - special cases:
# - Single \x0a is valid utf8, but should be hexed
#-----------------------------------------------------------------------
while (<>) {
chomp;
# If it looks like utf8, print it.
if (eval "\$test = decode( 'utf8', \$_, Encode::FB_CROAK )") {
print $_;
} else {
# If it isn't in the 7-bit ASCII printable range, HEX it.
if ( /[^\x20-\x39\x3b-\x7e]/ ) {
print '$HEX[' . unpack("H*", $_) . ']';
} else {
print $_;
}
}
print "\n";
}
#-----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment