pack, unpack and 64-bit values

Discussion in 'Perl Misc' started by Bill, Feb 9, 2006.

  1. Bill

    Bill Guest

    According to the perlfunc perl docs, the 'Q' in format string allows
    64-bit entities to be used in pack and unpack:

    my $binary = pack 'Q', $big_number;


    my @int64 = unpack 'Q*', $buffer;



    Two questions:

    1. Is the 'Q' network order, or machine order in its bytes?

    2. Can anyone show an efficient code snippet to emulate the above in
    32-bit perl versions that may not support 64-bit pack and unpack?
     
    Bill, Feb 9, 2006
    #1
    1. Advertising

  2. Bill

    Big and Blue Guest

    Bill wrote:

    > According to the perlfunc perl docs, the 'Q' in format string allows
    > 64-bit entities to be used in pack and unpack:
    >
    > 1. Is the 'Q' network order, or machine order in its bytes?


    It will be in machine order. Network order only applies to 16- and 32-
    bit quantities, as in the ntohl and ntohs macros, as network code doesn't
    use 64-bit ones

    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
     
    Big and Blue, Feb 9, 2006
    #2
    1. Advertising

  3. Big and Blue wrote:
    > Bill wrote:
    >
    >> According to the perlfunc perl docs, the 'Q' in format string allows
    >> 64-bit entities to be used in pack and unpack:
    >>
    >> 1. Is the 'Q' network order, or machine order in its bytes?

    >
    >
    > It will be in machine order. Network order only applies to 16- and
    > 32- bit quantities, as in the ntohl and ntohs macros, as network code
    > doesn't use 64-bit ones
    >


    I was afraid of that. So I guess to be network portable you must process
    pack and unpack of integer binary values as 32-bit chunks tops.
     
    William Herrera, Feb 10, 2006
    #3
  4. Bill

    Bart Lateur Guest

    William Herrera wrote:

    >I was afraid of that. So I guess to be network portable you must process
    >pack and unpack of integer binary values as 32-bit chunks tops.


    Not necessarily... though it would be the most portable solution, as
    most platforms don't support "Q".

    You could test the machine's endianness, stuff it into a constant, and
    simply reverse the 8 byte packed string before unpacking it with "Q".

    use constant LITTLE_ENDIAN => unpack "C", pack "s", 1;
    my $s = pack "C*", 0 .. 7;
    my $int = unpack "Q", LITTLE_ENDIAN ? $s : reverse $s;

    Testing this with B::Deparse, using "perl -MO=Deparse test.pl" produces
    the following output on 5.6.1/Win32:

    sub LITTLE_ENDIAN () {
    package constant;
    $scalar;
    }
    my $s = pack('C*', (0, 1, 2, 3, 4, 5, 6, 7));
    my $int = unpack('Q', $s);

    test.pl syntax OK

    So it gets resolved at compile time: there's no runtime penalty.

    --
    Bart.
     
    Bart Lateur, Feb 10, 2006
    #4
    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. joe shoemaker

    Python pack and unpack question

    joe shoemaker, Jul 15, 2008, in forum: Python
    Replies:
    0
    Views:
    320
    joe shoemaker
    Jul 15, 2008
  2. Bill Atkins
    Replies:
    6
    Views:
    138
    Daniel Berger
    Jun 14, 2004
  3. Anton Hörnquist

    Array#pack and String#unpack query

    Anton Hörnquist, Sep 11, 2008, in forum: Ruby
    Replies:
    2
    Views:
    110
    Jan Dvorak
    Sep 11, 2008
  4. Tulan W. Hu

    pack and unpack question for perl 5.8.0

    Tulan W. Hu, Jul 28, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    107
    Tulan W. Hu
    Jul 28, 2003
  5. Steve

    pack and unpack ?

    Steve, Jan 9, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    128
Loading...

Share This Page