in reply to Binary Reading Questions

You will want to look at the pack and unpack functions if you are routinely converting between numbers and their binary representation as characters.

Usually, Perl does the Right Thing, but if you want to be safe from Unicode encodings, use the binmode() call after opening your file, so you get full control over what Perl writes to the file:

open my $fh, "<", $fname or die "Couldn't open $fname: $!"; binmode $fh; seek $fh, 258, 0; read $fh, (my $buf), 1; $buf = unpack "c", $buf; ...

Why do you want to convert the stuff from the character to the integer at all? If there are no calculations to be made, you will likely be faster by not converting at all. But still, the IO overhead of reading the file will likely dwarf any optimizations you might make in your code ...

Update: PodMaster pointed me to Pack/Unpack Tutorial (aka How the System Stores Data) by pfaut - you should read that tutorial.

Update 2: frodo72 pointed out a typo in binmmode - thanks frodo72!

Replies are listed 'Best First'.
Re^2: Binary Reading Questions
by Eyeth (Sexton) on Jun 30, 2005 at 14:06 UTC
    Hello.

    Quote:
    Why do you want to convert the stuff from the character to the integer at all? If there are no calculations to be made, you will likely be faster by not converting at all. But still, the IO overhead of reading the file will likely dwarf any optimizations you might make in your code ...

    Thanks for the information. I've never thought about this I/O overhead in that way. Makes me more comfortable about using the ord/chr functions a whole lot more.

    As for your other question, this Perl script needs to handle binary files from anywhere of 2Mb to 16Mb (maximum). I need to utilize bitwise operators extensively, to seperate out bitfields suitable for use within Perl. Here's an example:

    $type = ord (substr ($entry, 0, 1)) & 191; # get filetype & remove file lock flag
    Also, thank you for pointing me into the direction of pfaut's Pack/Unpack Tutorial. I've already found it quite illuminating. Further study on my end is needed to see if the unpack function can efficiently carve up bitfields.

    I'll use binmode from now on.

    Enjoy.

      You want to look into vec.

      $type = vec($entry,0,8) & 191;

      The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon
        Talk about nailing it on the head! Thanks, idsfa! The vec function is just tailor-made for my Perl script.
Re^2: Binary Reading Questions
by BrowserUk (Patriarch) on Jun 30, 2005 at 13:17 UTC

    Why do you recommend binmode over :raw?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      I'm not too familiar with the PerlIO stuff, but it sure isn't in Perl 5.5.3 (but open my $fh isn't either), and I'm not sure if it is in Perl5.6.1. In any case it is necessary that the Perl is built with the slower PerlIO layer and I suspect weird distros like RedHat to build a Perl without it.