Bug #74748 [Opn->Nab]: HexDec outputs invalid conversions
| From: | danack@php.net | Date: | Mon, 12 Jun 2017 09:35:35 +0000 |
| Subject: | Bug #74748 [Opn->Nab]: HexDec outputs invalid conversions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209497@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74748&edit=1
ID: 74748
Updated by: danack@php.net
Reported by: sysop at pkx dot pw
Summary: HexDec outputs invalid conversions
-Status: Open
+Status: Not a bug
Type: Bug
Package: Math related
Operating System: LInux Mint 18
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
You are converting decimal to hex, which converts the number to the representation, but doesn't
save the signed information.
$hexed = dechex(-9223372036854775805);
var_dump($hexed);
var_dump(dechex(PHP_INT_MAX));
//Output is
string(16) "8000000000000003"
string(16) "7fffffffffffffff"
When PHP goes to convert back from this string, to a decimal, it interprets 0x80000000003 as an
unsigned number. As that number is bigger than PHP_INT_MAX, it overflows to a floating point number,
where weird things start happening, as not every int can be represented in a float above 2**53.
Perhaps you want to be using the pack and unpack functions:
http://php.net/manual/en/function.pack.php
http://php.net/manual/en/function.unpack.php
Which shouldn't make assumptions about the signedness of numbers.
Previous Comments:
------------------------------------------------------------------------
[2017-06-12 08:38:46] sysop at pkx dot pw
Description:
------------
---
From manual page: http://www.php.net/function.hexdec
---
Test script:
---------------
var_dump((int)hexdec(dechex(-9223372036854775805)));
var_dump((int)hexdec(dechex(-9223372036854775804)));
var_dump((int)hexdec(dechex(-9223372036854775803)));
Expected result:
----------------
int(-9223372036854775808)
int(-9223372036854775808)
int(-9223372036854775808)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74748&edit=1