Skip to content

Instantly share code, notes, and snippets.

@ltriant
Created May 12, 2020 12:02
Show Gist options
  • Save ltriant/d55f778dbbe3edd4d99fc19ef8a6487f to your computer and use it in GitHub Desktop.
Save ltriant/d55f778dbbe3edd4d99fc19ef8a6487f to your computer and use it in GitHub Desktop.
hexdump.pl
#!/usr/bin/env perl
# A hex dumping function I use for work purposes.
use warnings;
use strict;
hex_dump("ABCDEFGHIJKLMNOPQRTUVWXYZ0123456789");
sub hex_dump {
my ($payload) = @_;
my @octets = split '', $payload;
while (@octets) {
my @bits = splice @octets, 0, 16;
foreach my $bit (@bits) {
printf "%02s ", unpack("H2", $bit);
}
print " ";
if (scalar(@bits) < 16) {
my $extra_spaces = 16 - scalar(@bits);
print ' ' x $extra_spaces;
}
foreach my $bit (@bits) {
if ($bit =~ /[[:print:]]/) {
print $bit;
} else {
print '.';
}
}
print "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment