How to convert 64 bit unsigned integer to Hex value in php?
I'm getting events posted via an API as 64 bit unsigned integers and need to convert it hex values using php to identify all events that took place.
Some examples of events/integers that I've received looked like this; 80001000000 or 80010000000 or 80000000000
The output/hex values that I'm looking for is explained in this image (Cropped image):
Answer
Solution:
If anyone ever need the solution to this:
<?php
class Decoder {
public static function decode($event) {
$decimal = hexdec("$event");
$binary = decbin("$decimal");
$split_binary = str_split($binary);
$binary_length = strlen($binary) - 1;
$indicators = array();
$index = 0;
foreach ($split_binary as $letter) {
if ($letter == '1')
{
$signal = $binary_length - $index;
array_push($indicators, $signal);
}
$index ++;
}
return $indicators;
}
}
$events = array('40010000000', '40002000000', '80000000000');
foreach ($events as $event) {
print_r(Decoder::decode($event));
}
?>
Credit to the developer of the software.
Source