CUFP
kschwab
snippet
<div class="Description">subroutine to print a hexdump of a string. I saw
some discussion in the CB about needing such a thing.<p>
<b>Update:</b> Also see [id://46188|HexView] by [OeufMayo].</div>
<CODE>
sub hdump {
my $offset = 0;
my(@array,$format);
foreach my $data (unpack("a16"x(length($_[0])/16)."a*",$_[0])) {
my($len)=length($data);
if ($len == 16) {
@array = unpack('N4', $data);
$format="0x%08x (%05d) %08x %08x %08x %08x %s\n";
} else {
@array = unpack('C*', $data);
$_ = sprintf "%2.2x", $_ for @array;
push(@array, ' ') while $len++ < 16;
$format="0x%08x (%05d)" .
" %s%s%s%s %s%s%s%s %s%s%s%s %s%s%s%s %s\n";
}
$data =~ tr/\0-\37\177-\377/./;
printf $format,$offset,$offset,@array,$data;
$offset += 16;
}
}
</CODE>
<b>Update:</b> Fixed the map() in a void context problem.
Thanks [merlyn]. For the curious, it sort of evolved into
a void context as I cleaned up the subroutine before posting
it to perlmonks.