使用PHP十六进制值的大十六进制值 [英] Large hex values with PHP hexdec

查看:157
本文介绍了使用PHP十六进制值的大十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些很大的HEX值,我想以常规数字的形式显示,我使用的是hexdec()来转换为浮点数,并且我在PHP.net上找到一个函数将它转换为十进制数,但它似乎击中了天花板,例如:

  $ h ='D5CE3E462533364B'; 
$ f = hexdec($ h);
echo $ f。'='。 Exp_to_dec($ F);

输出:1.5406319846274E + 19 = 15406319846274000000

来自calc.exe的结果= 15406319846273791563



是否有另一种方法可以转换大的十六进制值?

解决方案

正如 hexdec手册页面所述:


该函数现在可以将值
转换为平台
整数类型的大值,它将返回值
作为float在这种情况下。


如果您想获得某种大整数(不是浮点数),您需要将其存储在串。这可以使用 BC数学功能。



例如,如果您查看hexdec手册页的注释,您会发现本笔记



如果您稍微调整了这个函数,为了避免通知,您会得到: p

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
$ len = strlen($ hex);
for($ i = 1; $ i <= $ len; $ i ++){
$ dec = bcadd($ dec,bcmul(strval(hexdec($ hex [$ i - 1])) ),bcpow('16',strval($ len - $ i))));
}
返回$ dec;
}

(该函数已从我链接到的笔记复制而来;只有一点点适合我)



并在您的电话号码上使用它:

  $ h ='D5CE3E462533364B'; 
$ f = bchexdec($ h);
var_dump($ f);

输出结果如下:

<$ p $






$ b $你有一种大浮动;并且对您所期望的行为看起来不错:


Calc.exe的结果=
15406319846273791563




希望得到这个帮助;-)

有时,PHP文档的用户注释有时候一个真正的金矿; - )

I have some large HEX values that I want to display as regular numbers, I was using hexdec() to convert to float, and I found a function on PHP.net to convert that to decimal, but it seems to hit a ceiling, e.g.:

$h = 'D5CE3E462533364B';
$f = hexdec($h);
echo $f .' = '. Exp_to_dec($f);

Output: 1.5406319846274E+19 = 15406319846274000000

Result from calc.exe = 15406319846273791563

Is there another method to convert large hex values?

解决方案

As said on the hexdec manual page:

The function can now convert values that are to big for the platforms integer type, it will return the value as float instead in that case.

If you want to get some kind of big integer (not float), you'll need it stored inside a string. This might be possible using BC Math functions.

For instance, if you look in the comments of the hexdec manual page, you'll find this note

If you adapt that function a bit, to avoid a notice, you'll get:

function bchexdec($hex)
{
    $dec = 0;
    $len = strlen($hex);
    for ($i = 1; $i <= $len; $i++) {
        $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
    }
    return $dec;
}

(This function has been copied from the note I linked to; and only a bit adapted by me)

And using it on your number:

$h = 'D5CE3E462533364B';
$f = bchexdec($h);
var_dump($f);

The output will be:

string '15406319846273791563' (length=20)

So, not the kind of big float you had ; and seems OK with what you are expecting:

Result from calc.exe = 15406319846273791563


Hope this help ;-)
And, yes, user notes on the PHP documentation are sometimes a real gold mine ;-)

这篇关于使用PHP十六进制值的大十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆