I often need hexdumps of files or output streams. So I wrote these 2 little helpers years ago. Maybe others find them useful too (and because neversaint asked me in th CB to post them...)
###### xd -- hexdump #!/usr/bin/perl @fill= qw( .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ); if (scalar(@ARGV)) { foreach $file(@ARGV) { $size= -s $file; $form= length(sprintf("%x",$size))-1; ++$form unless $form; print "File: ",$file,"\n"; open (IN,$file); &dump(*IN, $form); close (IN) } } else { &dump(*STDIN, 4); } sub dump { local(*IN, $form)= @_; my ($buf, $i); while ($rb= read(IN, $buf, 16)) { @x= unpack("H2" x $rb, $buf); $buf=~ s/[\x00-\x1f\x7f-\xff]/./g; $buf.= ' ' x (16-length($buf)); printf ("%0${form}x0: %s [%s]\n" ,$i++, ,sprintf ("%s %s %s %s %s %s %s %s - %s %s %s %s %s %s %s %s +", @x, @fill) ,$buf ); } } ###### dx -- undump the dump #!/usr/bin/perl if (scalar(@ARGV)) { foreach $file(@ARGV) { open (IN,$file); &undump(*IN); close (IN) } } else { &undump(*STDIN); } sub undump { local(*IN)= @_; local($_); while (<IN>) { # next unless s/^([0-9a-f]+)://; s/^([0-9a-f]+)://i; s/[^\-\s0-9a-f].*$//i; @match= /(\b[0-9a-f][0-9a-f]\b)/ig; print pack("H2" x scalar(@match), @match); } }

Replies are listed 'Best First'.
Re: xd and dx -- hexdump for debugging purposes
by Anonymous Monk on Jul 24, 2006 at 07:55 UTC

      Use of modules? Go ahead! Change it however you like. As I said: I wrote it ages ago. OTOH: Why use a module if a few lines can do?

      regarding binmode: Are you sure I need it anywhere else but on Windows, if I don't have to apply any special layer like utf8? I don't use windows. But as I said before: Go ahead: Change it however you like.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e