Converting a 4 byte field to integer.

Discussion in 'Perl Misc' started by Shashank Khanvilkar, Jul 29, 2005.

  1. 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
     
    Shashank Khanvilkar, Jul 29, 2005
    #1
    1. Advertising

  2. Shashank Khanvilkar

    Paul Lalli Guest

    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
     
    Paul Lalli, Jul 29, 2005
    #2
    1. Advertising

  3. Shashank Khanvilkar <> 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
     
    Sherm Pendley, Jul 29, 2005
    #3
    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. Kirby
    Replies:
    3
    Views:
    679
    Kirby
    Oct 8, 2004
  2. Polaris431
    Replies:
    8
    Views:
    795
    SM Ryan
    Dec 4, 2006
  3. Replies:
    2
    Views:
    180
    Chris Richmond - MD6-FDC ~
    Apr 4, 2006
  4. Sound
    Replies:
    2
    Views:
    499
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    497
Loading...

Share This Page