package HexDump ; =pod =head1 Synopsis Quick program to convert strings into 'dumps' of their hex values. Useful for debugging pack, unpack and vec usages. use HexDump ; my($hexDump) = hd("Hello World") ; =head1 Example: my $str = "Hello World\n" ; print "$str", HexDump::hd($str), "\n" ; =cut require Exporter; our @ISA = qw(Exporter); use vars qw/@EXPORT/ ; @EXPORT = qw/hd/ ; my($hdWidth) = int($ENV{HEXDUMP_WIDTH}) || 8 ; ## ## converts non printable chars to '.' for a string ## sub printablestr { return join "", map { (ord($_) >= 32 && ord($_) < 127) ? $_ : '.' } split //, $_[0] ; } ## ## hex dump utility function ## sub hd { my(@retList) ; my($width) = $hdWidth ; my($offset) = 0 ; my($len, $fmt, $n, @elems) ; for( @_ ) { my($str) ; $len = length $_ ; while($len) { $n = $len >= $width ? $width : $len ; $fmt = "\n%04X " . ("%02X " x $n ) . ( ' ' x ($width - $n) ) . " %s" ; @elems = map ord, split //, (substr $_, $offset, $n) ; $str .= sprintf($fmt, $offset, @elems, printablestr(substr $_, $offset, $n)) ; $offset += $width ; $len -= $n ; } # while push @retList, $str ; } # for return $retList[0] unless wantarray ; return @retList ; } # end of hd 1 ;