Created
May 12, 2020 12:02
-
-
Save ltriant/d55f778dbbe3edd4d99fc19ef8a6487f to your computer and use it in GitHub Desktop.
hexdump.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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