Reading binary files as numbers?

Discussion in 'Perl Misc' started by Jomar Bueyes, Sep 23, 2009.

  1. Jomar Bueyes

    Jomar Bueyes Guest

    Hi,

    I'm trying to read a binary file made of 4-byte integers. However,
    Perl seems to be interpreting the the four bytes as four characters,
    not one number. I've unsuccessfully looked in perldoc -q binary,
    perldoc -[fq] read, searched this newsgroup for "read binary", "read
    binary number", ...

    When I run the simplified program below, I get a warning that the
    argument "$header[$k]" is not numeric.

    Could you please let me know how can I make Perl interpret the four-
    byte words as integers or how to convert the characters to their
    equivalent signed integer? (later I'll also need floating point).
    ----
    #!/usr/bin/perl
    use strict;
    use warnings;
    my $filename = $ARGV[0];
    my @header;

    open(FH, '<:raw', $filename) || die("cound not open $filename: $!\n");
    # I tried also w/o the ":raw", same results
    for ( my $k = 0; $k<4; $k++){
    my $count = read(FH, $header[$k], 4) ||
    die("Could not read from $filename: $!\n");
    ### print "count = $count\n";
    printf "Header[%d] = %d\n", $k, $header[$k];
    }
    close(FH);
    ----
    Thanks in advance
     
    Jomar Bueyes, Sep 23, 2009
    #1
    1. Advertising

  2. Jomar Bueyes <> wrote:
    >I'm trying to read a binary file made of 4-byte integers. However,
    >Perl seems to be interpreting the the four bytes as four characters,
    >not one number. [...]
    >Could you please let me know how can I make Perl interpret the four-
    >byte words as integers or how to convert the characters to their
    >equivalent signed integer? (later I'll also need floating point).



    perldoc -f pack
    perldoc -f unpack

    jue
     
    Jürgen Exner, Sep 23, 2009
    #2
    1. Advertising

  3. Jomar Bueyes

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Quoth Jomar Bueyes <>:
    >>
    >> I'm trying to read a binary file made of 4-byte integers. However,
    >> Perl seems to be interpreting the the four bytes as four characters,
    >> not one number. I've unsuccessfully looked in perldoc -q binary,
    >> perldoc -[fq] read, searched this newsgroup for "read binary", "read
    >> binary number", ...
    >>
    >> When I run the simplified program below, I get a warning that the
    >> argument "$header[$k]" is not numeric.
    >>
    >> Could you please let me know how can I make Perl interpret the four-
    >> byte words as integers or how to convert the characters to their
    >> equivalent signed integer? (later I'll also need floating point).


    BM> perldoc -f unpack

    BM> You'll also need to read perldoc -f pack for the format characters.

    and in recentish perls there is perlpacktut which is very helpful. the
    pack docs are very terse and somewhat hard to understand if you don't
    know c and machine level stuff.

    uri
     
    Uri Guttman, Sep 23, 2009
    #3
  4. Jomar Bueyes

    Jomar Bueyes Guest

    On Sep 23, 5:16 pm, "Uri Guttman" <> wrote:
    > >>>>> "BM" == Ben Morrow <> writes:

    >
    >   BM> Quoth Jomar Bueyes <>:
    >   >>
    >   >> I'm trying to read a binary file made of 4-byte integers. However,
    >   >> Perl seems to be interpreting the the four bytes as four characters,
    >   >> not one number.  I've unsuccessfully looked in perldoc -q binary,
    >   >> perldoc -[fq] read, searched this newsgroup for "read binary", "read
    >   >> binary number", ...
    >   >>
    >   >> When I run the simplified program below, I get a warning that the
    >   >> argument "$header[$k]" is not numeric.
    >   >>
    >   >> Could you please let me know how can I make Perl interpret the four-
    >   >> byte words as integers or how to convert the characters to their
    >   >> equivalent signed integer? (later I'll also need floating point).
    >
    >   BM> perldoc -f unpack
    >
    >   BM> You'll also need to read perldoc -f pack for the format characters.
    >
    > and in recentish perls there is perlpacktut which is very helpful. the
    > pack docs are very terse and somewhat hard to understand if you don't
    > know c and machine level stuff.
    >
    > uri


    Thank you Uri, Ben, and Jürgen

    Jomar
     
    Jomar Bueyes, Sep 23, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Gowans

    Reading/Writing pure binary files

    Daniel Gowans, May 27, 2004, in forum: VHDL
    Replies:
    2
    Views:
    5,279
  2. Daniel Moree

    Reading and Writing to Binary Files

    Daniel Moree, Nov 23, 2004, in forum: C++
    Replies:
    9
    Views:
    1,023
    Jonathan Mcdougall
    Nov 24, 2004
  3. Rakesh Sinha
    Replies:
    2
    Views:
    570
    Jonathan Mcdougall
    Dec 28, 2004
  4. Ron Natalie
    Replies:
    2
    Views:
    1,575
    WahJava
    Mar 2, 2005
  5. Jim
    Replies:
    6
    Views:
    765
Loading...

Share This Page