Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
laziness, impatience, and hubris
 
PerlMonks  

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

by JFarr (Sexton)
on Nov 02, 2005 at 21:49 UTC ( #505122=perlquestion: print w/ replies, xml ) Need Help??
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
Re: how to read in a binary file and convert it to ascii output
by GrandFather (Cardinal) 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 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 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.

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

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

        -derby
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://505122]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-04-21 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls