Только что посмотрел код для Zend_Crypt_Math_BigInteger_Bcmath
и Zend_Crypt_Math_BigInteger_Gmp
, который решает эту проблему:
Использование BCmath (с обратным порядком байтов)
По сути, это решение, опубликованное 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;
}
Использование GMP (с обратным порядком байтов)
Тот же алгоритм - просто разные имена функций.
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);
}
Изменить алгоритм для использования порядка байтов Litte-Endian довольно просто: просто прочитайте двоичные данные от конца к началу:
Использование BCmath (с обратным порядком байтов)
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;
}
Использование GMP (буквенный порядок байтов)
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);
}
person
Stefan Gehrig
schedule
08.04.2009