Just looked at the code for Zend_Crypt_Math_BigInteger_Bcmath and Zend_Crypt_Math_BigInteger_Gmp which deals with this problem:
Using BCmath (Big-Endian)
This is essentially a solution posted by Chad Birch .
public static function bc_binaryToInteger($operand) { $result = '0'; while (strlen($operand)) { $ord = ord(substr($operand, 0, 1)); $result = bcadd(bcmul($result, 256), $ord); $operand = substr($operand, 1); } return $result; }
Using GMP (Big-Endian)
The same algorithm - just different function names.
public static function gmp_binaryToInteger($operand) { $result = '0'; while (strlen($operand)) { $ord = ord(substr($operand, 0, 1)); $result = gmp_add(gmp_mul($result, 256), $ord); $operand = substr($operand, 1); } return gmp_strval($result); }
Changing the algorithm to use the Litte-Endian byte order is quite simple: just read the binary data from the beginning and the end:
Using BCmath (Litte-Endian)
public static function bc_binaryToInteger($operand) { // Just reverse the binray data $operand = strrev($operand); $result = '0'; while (strlen($operand)) { $ord = ord(substr($operand, 0, 1)); $result = bcadd(bcmul($result, 256), $ord); $operand = substr($operand, 1); } return $result; }
Using GMP (Litte-Endian)
public static function gmp_binaryToInteger($operand) { // Just reverse the binray data $operand = strrev($operand); $result = '0'; while (strlen($operand)) { $ord = ord(substr($operand, 0, 1)); $result = gmp_add(gmp_mul($result, 256), $ord); $operand = substr($operand, 1); } return gmp_strval($result); }