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

JFarr has asked for the wisdom of the Perl Monks concerning the following question:

I'm stuck trying to figure out how to read in a binary file, then convert it to ascii output to display on the screen. Can someone help? Thanks in advance.
  • Comment on how to read in a binary file and convert it to ascii output

Replies are listed 'Best First'.
Re: how to read in a binary file and convert it to ascii output
by sauoq (Abbot) on Nov 02, 2005 at 22:03 UTC

    Here's a one-liner...

    perl -0777e'$i++,print $_.($i%20?" ":"\n")for unpack"(H2)*",<>;print"\ +n"' file

    Update: Cleaned up and shortened the one-liner. I'm just assuming that you want a hex viewer. You weren't very explicit.

    Update2: a little more squishing.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: how to read in a binary file and convert it to ascii output
by Fletch (Bishop) on Nov 02, 2005 at 22:06 UTC
Re: how to read in a binary file and convert it to ascii output
by Skeeve (Parson) on Nov 02, 2005 at 22:51 UTC

    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
      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
Re: how to read in a binary file and convert it to ascii output
by GrandFather (Saint) on Nov 02, 2005 at 22:01 UTC

    Perhaps you need to be a little more explicit about the nature of the binary file and the way you want to show it in ASCII. For example, do you simple want to run through the file byte by byte and show the hex values for each byte? It may help to tell us what you are trying to achieve.


    Perl is Huffman encoded by design.
Re: how to read in a binary file and convert it to ascii output
by chas (Priest) on Nov 02, 2005 at 22:06 UTC
    Depends on what the binary file contains. If it is, for example, an image file then you're not likely to output it as text to display in a reasonable way. If it is an encoding of text in some binary format then some unencoding would be required, but without more details it's hard to say.
    chas
      If it is, for example, an image file then you're not likely to output it as text to display in a reasonable way

      Sure, just use aalib. HTH.HAND.