Bug #76098 [NEW]: Conversion from hex string to 64-bit int returns wrong value
From: mikael dot knutsson at gmail dot com
Operating system: Linux 64-bit
PHP version: 7.2.3
Package: Variables related
Bug Type: Bug
Bug description:Conversion from hex string to 64-bit int returns wrong value
Description:
------------
We have a hex string like this: "eb3dd796efda2409" received from an
upstream system. This is a big-nibble hex string containing this integer
value: -1495802457948019703
This is a 64-bit signed integer.
When converting this using hexdec this is converted to a float and
returns the wrong value, when assigned to a variable as a hex string and
converted using intval() it loses precision.
When looking at the documentation:
http://php.net/manual/en/function.hexdec.php
it mentions this:
> The function can convert numbers that are too large to fit into the
platforms integer type, larger values are returned as float in that
case.
However, this happens on a 64-bit compiled PHP where this integer should
fit into the platform's integer size.
php --version
PHP 7.2.3-1 (cli) (built: Mar 6 2018 11:15:04) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.3-1, Copyright (c) 1999-2018, by Zend
Technologies
php -a
Interactive mode enabled
php > echo PHP_INT_MAX;
9223372036854775807
php > echo PHP_INT_SIZE;
8
As a side note:
We ended up using pack/unpack to avoid the interim float conversion, and
we also had to use "J" as the unpack conversion type to get the right
value which implies big-endianness in the packed value. I am not sure
this is correct either as doing this conversion in other languages works
just fine without specifying endianness. (tried in Python and Go)
(I can file a separate bug if this is also not intended)
Test script:
---------------
$strhex = "eb3dd796efda2409";
$hex = 0xeb3dd796efda2409;
echo hexdec($strhex);
echo $hex;
echo intval($hex);
var_dump(unpack('J', pack("H*", $strhex)));
Expected result:
----------------
-1495802457948019703
-1495802457948019703
-1495802457948019703
array(1) {
[1]=>
int(-1495802457948019703)
}
Actual result:
--------------
1.6950941615762E+19
1.6950941615762E+19
-1495802457948020736
array(1) {
[1]=>
int(-1495802457948019703)
}
--
Edit bug report at https://bugs.php.net/bug.php?id=76098&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76098&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76098&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76098&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=76098&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=76098&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=76098&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=76098&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=76098&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=76098&r=support
Expected behavior: https://bugs.php.net/fix.php?id=76098&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=76098&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=76098&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=76098&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76098&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=76098&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=76098&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=76098&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=76098&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=76098&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=76098&r=mysqlcfg
Thread (1 message)
- mikael dot knutsson at gmail dot com