FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » signed int64 pack/unpack
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: signed int64 pack/unpack [message #184598 is a reply to message #184577] Sun, 12 January 2014 16:37 Go to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
cameron7(at)gmail(dot)com wrote:

> I'm having a bit of trouble figuring out a straightforward way to
> "pack" / "unpack" a signed int64.
>
> I'm on a 64-bit machine, but the 'i' and 'I' are coming back with
> 4-byte length. And of course even though double comes back with 8
> length it's a floating point and thus loses precision.
>
> Any thoughts? Any help would be greatly appreciated.
>
> BTW, unsigned int64 seems relatively straightforward, but signed
> int64 has been unusually difficult to figure out.

It seems to me that it should be possible to pack 64bit numbers by
splitting them into two 32bit numbers and packing the "halves", as Ben
already noticed. Unpacking should be possible by doing the reverse
operations. It shouldn't really matter in this case if you're dealing
with signed or unsigned values, if your platform supports the respective
64bit values.

I can't test with 64bit Integers as I'm on Windows with
PHP_INT_MAX==2147483647. However, I've written the following functions
for working with 32bit integers by relying on the 'n' format of
pack()/unpack(). You may try to adjust the functions for 64bit by using
the 'N' format and replacing the literal "16" with "32".

function packInt($number)
{
$high = $number >> 16;
$low = $number & ((1 << 16) - 1);
return pack('n2', $high, $low);
}

function unpackInt($string)
{
$strings = unpack('n2', $string);
return ($strings[1] << 16) | $strings[2];
}

--
Christoph M. Becker
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: how did I wind up with double headers?
Next Topic: Switch question
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 19:28:34 GMT 2024

Total time taken to generate the page: 0.05067 seconds