in reply to pack 64 bit int ?

I came up with this for unpacking 64-bit integers into Perl's native scalars.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: pack 64 bit int ?
by frankied (Beadle) on May 01, 2002 at 21:05 UTC
    (wondering how I missed that node when searching)

    I did this for unpack:

    my ($int1,$int2)=unpack('NN',shift()); my $sign=($int1&0x80000000); if($sign) { $int1^=-1; $int2^=-1; ++$int2; $int2%=2**32; ++$int1 unless $int2; } my $i=new Math::BigInt $int1; $i*=2**32; $i+=$int2; $i=-$i if $sign;
    your 2's complement conversion looks less painful, though.

    update:
    shamelessly (thank you!) stealing ideas from tye's unpack, I would now write down a pack like:

    my $i=new Math::BigInt shift(); my($int1,$int2)=do { if ($i<0) { $i=-1-$i; (~(int($i/2**32)%2**32),~int($i%2**32)); } else { (int($i/2**32)%2**32,int($i%2**32)); } }; pack('NN',$int1,$int2);