Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Converting a 4 byte field to integer.

Reply
Thread Tools

Converting a 4 byte field to integer.

 
 
Shashank Khanvilkar
Guest
Posts: n/a
 
      07-29-2005
Hi,
All help is appreciated.
I have to read a binary file having a given structure (not important here).
One field that i need to read is a 4 byte field specifying some page
numeber.

If i want to display this page number, how can u do this?
The below fragment does not seem to work,

--SNIP--
read(IN, $buffer, 4); #IN is the fh for a file open in binary mode..
$page_number = ord($buffer);
print "[XXX] $page_number\n";
--SNIP--

For example, if $buffer contains the following four values,
0x74 0x29 0x00 0x00
then
print will print
[XXX] 116
which is dec(0x74)while I want dec(0x74|0x29|0x00|0x00);
(do not worry about the endianess at this point.)

Thanks
Shank

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-29-2005
Shashank Khanvilkar wrote:
> All help is appreciated.
> I have to read a binary file having a given structure (not important here).
> One field that i need to read is a 4 byte field specifying some page
> numeber.
>
> If i want to display this page number, how can u do this?
> The below fragment does not seem to work,


Just guessing here, but I bet you want to look into pack and/or unpack
perldoc -f pack
perldoc -f unpack
perldoc perlpacktut

> --SNIP--
> read(IN, $buffer, 4); #IN is the fh for a file open in binary mode..
> $page_number = ord($buffer);
> print "[XXX] $page_number\n";
> --SNIP--
>
> For example, if $buffer contains the following four values,
> 0x74 0x29 0x00 0x00
> then
> print will print
> [XXX] 116
> which is dec(0x74)


.... which is exactly what
perldoc -f ord
says would happen.

Paul Lalli

 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      07-29-2005
Shashank Khanvilkar <(E-Mail Removed)> writes:

> The below fragment does not seem to work,
>
> --SNIP--
> read(IN, $buffer, 4); #IN is the fh for a file open in binary mode..
> $page_number = ord($buffer);


You need to get acquainted with pack() and unpack(). See:
perldoc -f pack
perldoc -f unpack
perldoc perlpacktut

In this case you'd use unpack(), like so:
my $page_number = unpack('L', $buffer);

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
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
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
Single byte addressable, multiple byte readout. Andreas VHDL 1 05-04-2004 01:49 PM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments