[Israel.pm] packing and unpacking large numbers

Yona Shlomo yona at cs.technion.ac.il
Sat Jul 28 21:03:23 PDT 2007


On Thu, 26 Jul 2007, Shmuel Fomberg wrote:

>
>> From: Yona Shlomo [mailto:yona at cs.technion.ac.il]
>> Sent: Thursday, July 26, 2007 3:43 PM
>
>> 3. pack the number into a C long long int data type (i.e., a
>> 64 bite representation).
>
> You can always take the stringy path, that is: (tested)
> $x = Math::BigInt->new('123456789123');
> $str = $x->as_bin();
> substr($str, 0, 2) = '0' x (64 - length($str) + 2);
> $binary = pack "B*", $str;
>
> just make sure that you have the right byte order.
> (big/little indeans and so)

Can you explain the operations?

I looks like you're padding with '0' characters the binary
representation in string format of the big number -- but I
don't understand the purpose of the
    substr($str, 0, 2) = '0' x (64 - length($str) + 2);
neither the lvalue nor the rvalue

Thanks

-- 
Shlomo Yona
yona at cs.technion.ac.il
http://yeda.cs.technion.ac.il/~yona/



More information about the Perl mailing list