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 ;

In reply to HexDump.pm by ptkdb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.