Antonix
Antonix

Reputation: 215

Converting hex to int64 in PHP

How to convert hex 48ea369a4c120000 to 64bit integer 20120214104648 (little endian byte order) in PHP?

hexdec(), base_convert() and unpack() does not help.

See screenshot below.

See screenshot below

Upvotes: 2

Views: 3187

Answers (2)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

There are 2 values of 48ea369a4c120000 if you consider endiannes.

Little endian    =        20120214104648
Big endian       =   5254071951610216448

The little endian value of 48ea369a4c120000 can be calculated by following process.

$data="48ea369a4c120000";
$u = unpack("H*", strrev(pack("H*", $data)));
$f = hexdec($u[1]);
echo $f; // 20120214104648

Note: This is manually done as the endianness of the host is different. if the host had same endianness, only a single call to hexdec was enough.

Upvotes: 3

Gleeb
Gleeb

Reputation: 166

You might try hexdec().

$integer = hexdec('48ea369a4c120000'); // $integer is now the decimal version of the number provided.

Note, however, that if the resultant number is outside the bounds than the platform's storage (e.g. 64 bits needed on a 32-bit platform) then you'll get a float, not an int.

Upvotes: 0

Related Questions