Reading binary files as numbers?

J

Jomar Bueyes

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);
 
J

Jürgen Exner

Jomar Bueyes said:
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
 
U

Uri Guttman

BM> Quoth Jomar Bueyes said:
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
 
J

Jomar Bueyes

  BM> Quoth Jomar Bueyes <[email protected]>:
  >>
  >> 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top