webdevask.com

Menu

  • Home
  • New
  • Add question

How to convert 64 bit unsigned integer to Hex value in php?

View Original

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

enter image description here

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

See also:

  • php สร้าง Google ชีตจากบัญชีบริการ

  • php - Laravel 8.0 Installation Failed

  • javascript - How to include HTML data (tags) as value in "arrayToDataTable" of Google Pie Chart?

  • how to Grouping array in php

  • php - vender folder from my laravel project has more 1k error

© 2021
E-mail: contact@webdevask.com
Back to top