") Update: Salva correctly indicates that I was mistaken regarding the introduction of the packa...">

Effective pre-perl-5.10 equivalent package ("Q>")

Update: Salva correctly indicates that I was mistaken regarding the introduction of the package template “Q”. This is the ">" modifier, which does not return to 5.8.

Perl 5.10 introduced the pack () ">" modifier, which for my use case with "Q" packs an unsigned square (64-bit) value in a large endian.

Now I am looking for an effective equivalent for

pack("Q>2", @ints)

where @ints contains two 64-bit unsigned ints. "Q> 2" means "a packet of two unsigned quads in byte order of bytes." Obviously, I want this because I'm (at least temporarily) bound to pre-5.10 Perl.

Update2: In fact, with further thought, the following should be done:

pack("N4", $ints[0] >> 32, $ints[0], $ints[1] >> 32, $ints[1])

Appears to work on my 64-bit x86-64 Linux. Any reason why this might not be exactly the same as pack("Q>2", @ints)? Any questions related to the platform?

What is the opposite (i.e. equivalent to unpacking ("Q> 2", @ints))?

+3
source share
1 answer

The template Qwas introduced in perl 5.6. Your real problem may be that you are trying to use it in perl compiled without 64-bit support.

In any case, you can use Math :: Int64 .

Refresh example:

use Math::Int64 qw(int64_to_native);
my $packed = join '', map int64_to_native($_), @ints;

Another option, if you are using 64-bit perl, which supports Q, but does not Q>, should change the byte order yourself:

pack 'C*', reverse unpack 'C*', pack 'Q', $int;
+5
source

All Articles