Let's say I have a byte-stream in which I know the location of a 64-bit value (a 64-bit nonce). The byte-order is Little-Endian. As PHP's integer data-type is limited to 32-bit (at least on 32-bit operating systems) how would I convert the byte-sequence into a PHP numeric representation (float would be sufficient I think)?
$serverChallenge = substr($bytes, 24, 8);
// $serverChallenge now contains the byte-sequence
// of which I know that it's a 64-bit value
Just looked up the code for
Zend_Crypt_Math_BigInteger_Bcmath
andZend_Crypt_Math_BigInteger_Gmp
which deals with this problem:Using BCmath (Big-Endian)
This is essentially the solution posted by Chad Birch.
Using GMP (Big-Endian)
Same algorithem - just different function names.
Changing the algorithem to use Litte-Endian byte-order is quite simple: just read the binary data from end to start:
Using BCmath (Litte-Endian)
Using GMP (Litte-Endian)
This seems like a total hack, but it should do the job, assuming you have the BC Math functions that daemonmoi recommended:
I know this is not quite the answer to the question, but check out the BC Math functions to handle big numbers.
Two years late to the party, but if anyone still cares: unpack is the built-in way to go here, you can unpack it as a couple of 32-bit ints, or as a double.