简体   繁体   中英

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):

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM