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

Hello Monks. I am seeking your wisdom on reading info in from a binary file. I have a binary file that I need to extract some text informtion from. The file contains text strings that I can see if I just open the file in notepad, but I can't seem to get that information if I read it in with a Perl script.

I am guessing I need to convert the data to HEX values which I have done by reading in the file and then printing it with this:

print unpack ('H*', $_), "\n";

The problem is I din't know what to do next to convert the HEX strings to text or if I am even on the right track. Any suggestions?

Thanks

--Prime

Replies are listed 'Best First'.
Re: Reading a binary file
by eyepopslikeamosquito (Archbishop) on Dec 30, 2004 at 07:36 UTC

    I do this all the time, works fine, just be sure to use binmode after you open the file, then slurp the whole file into a string (reading line-by-line makes no sense with a binary file). For example:

    use strict; open(my $fh, '<', 'some_binary_file') or die; binmode($fh); my $file_contents = do { local $/; <$fh> }; close($fh); $file_contents =~ /some-string/ and print "it matched\n";

Re: Reading a binary file
by duff (Parson) on Dec 30, 2004 at 07:49 UTC

    Why do you think you need to change the representation of the characters you've read? How you know when you've read the text that you are looking for? How are you reading the file? More information would help us help you. :-)

    BTW, if you're on a unixish platform, type "man strings" at your command prompt. "strings" is a ready-made program for extracting text strings from binary files.