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