Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.