Hex Dump with Perl Scripting
Want to Hex Dump with Perl? This snippet (not my own code), was too good to let vanish.
This script uses sprintf to format the chunks of data, and pack/unpack functions to convert to Hex.
I am saving this Perl Hex Dump Script in case you might need it - but if you want Custom Perl Scripting please contact us.
Run it by calling the script with the single argument of the file you want to hex dump in Perl:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX 'ceil';
for my $file (@ARGV) {
my $size = 16 * ceil( (-s $file) / 16);
my $form = length sprintf '%x', $size-1;
$form ||= 1;
print "File: $file\n";
open my $fh, '<', $file or die $!;
my $i = 0;
while ( my $rb = read $fh, my $buf, 16 ) {
my @x = unpack '(H2)*', $buf;
push @x, ' ' until @x == 16;
$buf =~ tr/\x20-\x7E/./c;
$buf .= ' ' x (16 - length($buf));
printf "%0*x: %s [%s]\n",
$form,
$i++,
sprintf( "%s %s %s %s %s %s %s %s - %s %s %s %s %s %s %s %s", @x ),
$buf;
}
print "\n";
}