http://qs321.pair.com?node_id=505163


in reply to how to read in a binary file and convert it to ascii output

An old script of mine will do this:

#!/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 ); } }
YES! No strict, No warnings... As I said above: It's old! I was younger that time... And not a monk!


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

Replies are listed 'Best First'.
Re^2: how to read in a binary file and convert it to ascii output
by JFarr (Sexton) on Nov 03, 2005 at 13:35 UTC
    Thank all of you for the help. That dumped out the file in hex, now I can parse through and get the ascii. Thanks again
      now I can parse through and get the ascii

      That makes it sound like you want to read through a file that contains both "printable" and "non-printable" byte values, and display only the printable (ascii) stuff. On unix, there is a command called "strings" to do exactly that.

      A perl implementation would be something like this (assuming the file is not too big to hold in memory):

      $_ = do { local $/; <> }; s/[^\t\n\r -~]+/\n/g; print;
      You could tweak that to print only strings that are some minimum length, etc. Anything else you know about the binary file (e.g. strings of interest are all terminated by null bytes, or something like that) could be used to good effect in making sure you only print the interesting parts.

      It sounds like you're looking for the strings command.

      -derby