I don't know how "Cool" this is, but I find this little hex dump subroutine useful from time to time when I'm dealing with data that I'm not familiar with.

Print a simple hex dump, hex on left side, dot-masked ASCII on the right side.

use strict; use warnings; my $string = "non-printable or ill-behaved characters: \x0d \x0a \x00 +\b \a"; # my $string = ''; print hexdump(\$string); sub hexdump { my $str = ref $_[0] ? ${$_[0]} : $_[0]; return "[ZERO-LENGTH STRING]\n" unless length $str; # split input up into 16-byte chunks: my @chunks = $str =~ /([\0-\377]{1,16})/g; # format and print: my @print; for (@chunks) { my $hex = unpack "H*", $_; tr/ -~/./c; # mask non-print chars $hex =~ s/(..)(?!$)/$1 /g; # insert spaces in hex # make sure our hex output has the correct length $hex .= ' ' x ( length($hex) < 48 ? 48 - length($hex) : 0 ); push @print, "$hex $_\n"; } wantarray ? @print : join '', @print; } __END__ prints: 6e 6f 6e 2d 70 72 69 6e 74 61 62 6c 65 20 6f 72 non-printable or 20 69 6c 6c 2d 62 65 68 61 76 65 64 20 63 68 61 ill-behaved cha 72 61 63 74 65 72 73 3a 20 0d 20 0a 20 00 20 08 racters: . . . . 20 07 .

conv

Update:
Changed substitution to tr/// at japhy's suggestion.

For what it's worth, I also have an IBM-style dump that is useful for fixed-width records where columnar alignment is desired. I'll post that one as soon as I find time.


In reply to A simple hex dump by converter

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.