Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reading binary files as numbers?

Reply
Thread Tools

Reading binary files as numbers?

 
 
Jomar Bueyes
Guest
Posts: n/a
 
      09-23-2009
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-23-2009
Jomar Bueyes <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      09-23-2009
>>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

BM> Quoth Jomar Bueyes <(E-Mail Removed)>:
>>
>> 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
 
Reply With Quote
 
Jomar Bueyes
Guest
Posts: n/a
 
      09-23-2009
On Sep 23, 5:16*pm, "Uri Guttman" <(E-Mail Removed)> wrote:
> >>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

>
> * BM> Quoth Jomar Bueyes <(E-Mail Removed)>:
> * >>
> * >> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: working with binary files/extract png from a binary file Jim Ruby 6 12-24-2013 08:09 AM
Error reading files in ios::binary mode Ron Natalie C++ 2 03-02-2005 12:11 PM
Reading bytes from binary files containing '0' character Rakesh Sinha C++ 2 12-28-2004 04:35 PM
Reading and Writing to Binary Files Daniel Moree C++ 9 11-24-2004 10:20 PM
Reading/Writing pure binary files Daniel Gowans VHDL 2 06-12-2004 01:25 AM



Advertisments