在32位系统上将编码为十六进制字符串的64位整数转换为十进制字符串的简单方法是什么?它必须是完整的值,它不能是科学记数法或截断的:/
“0c80000000000063”==“900719925474099299”
“0c80000000000063”!= 9.007199254741E + 17
PHP的base_convert()和hexdec()不能正确地完成工作。
答案 0 :(得分:2)
您需要使用BC Math PHP扩展(捆绑)。
首先拆分输入字符串以获得高字节和低字节,然后将其转换为十进制,然后通过BC函数进行计算,如下所示:
$input = "0C80000000000063";
$str_high = substr($input, 0, 8);
$str_low = substr($input, 8, 8);
$dec_high = hexdec($str_high);
$dec_low = hexdec($str_low);
//workaround for argument 0x100000000
$temp = bcmul ($dec_high, 0xffffffff);
$temp2 = bcadd ($temp, $dec_high);
$result = bcadd ($temp2, $dec_low);
echo $result;
/*
900719925474099299
*/
答案 1 :(得分:1)
您是否在php.net上看到了对hexdec帮助页面的第一条评论?
当给出大数字时,hexdec函数会自动转换 科学记数法的价值。所以,“aa1233123124121241”作为一个 十六进制值将转换为“3.13725790445E + 21”。如果你是 转换表示哈希值的十六进制值(md5或 sha),然后你需要该表示的每一位才能成功 有用。通过使用number_format函数,您可以这样做 完美。例如:
<?php
// Author: holdoffhunger@gmail.com
// Example Hexadecimal
// ---------------------------------------------
$hexadecimal_string = "1234567890abcdef1234567890abcdef";
// Converted to Decimal
// ---------------------------------------------
$decimal_result = hexdec($hexadecimal_string);
// Print Pre-Formatted Results
// ---------------------------------------------
print($decimal_result);
// Output Here: "2.41978572002E+37"
// .....................................
// Format Results to View Whole All Digits in Integer
// ---------------------------------------------
// ( Note: All fractional value of the
// Hexadecimal variable are ignored
// in the conversion. )
$current_hashing_algorithm_decimal_result = number_format($decimal_result, 0, '', '');
// Print Formatted Results
// ---------------------------------------------
print($current_hashing_algorithm_decimal_result);
// Output Here: "24197857200151253041252346215207534592"
// .....................................
?>