【发布时间】:2018-09-29 23:00:40
【问题描述】:
我正在研究添加几个十六进制值(base16)的东西。
在常规的 C 程序中,我执行以下操作:
uint32_t test1 = 0x5BE0CD19;
uint32_t test2 = 0x3587272B;
uint32_t test3 = 0x1F85C98C;
uint32_t test4 = 0x428A2F98;
uint32_t test5 = 0x61626380;
uint32_t test6 = (test1 + test2 + test3 + test4 + test5);
printf( "%08x \n", test6 );
结果是:
> 54da50e8
但是,在 PHP(我的 PHP 是 64 位)中,当我应用相同的总和时:
$test1 = 0x5BE0CD19;
$test2 = 0x3587272B;
$test3 = 0x1F85C98C;
$test4 = 0x428A2F98;
$test5 = 0x61626380;
$test6 = ($test1 + $test2 + $test3 + $test4 + $test5);
echo(sprintf( "%08x \n", $test6 ));
我明白了:
> 154da50e8
奇怪的是,这个数字是一个额外的字符,前面有一个1。
这种行为有什么原因吗?如果有,我该如何预防?
【问题讨论】:
-
PHP 打印出正确答案,你的 c-addition 刚刚溢出。
标签: php hex integer-overflow