Convert milliseconds to 16-digit hexadecimal

Asked

Viewed 65 times

2

I am implementing an algorithm to generate an access key to a given system, however the need to convert a numerical value to 16-digit hexadecimal.

Consider the number of milliseconds elapsed since January 1, 1970:

$milliseconds = (time()*1000);
//1530747443000

It is possible to convert the value in milliseconds to a 16-digit hexadecimal?

  • @Felipemoraes the highest converted value is 4294967295, and the variable $milliseconds is more than 300x larger. Can’t be a variable parts conversion to form a 16-digit hexa?

  • 1

    @Weessmith The instructions to create the algorithm tells you to get the number of seconds elapsed since January 1970 and then tells you to convert the milliseconds into a 16-digit hexadecimal. I’ve tried using PHP functions that do this conversion but I couldn’t get 16 digits, just adding random characters.

  • Boy, I’m racking my brain trying to find a 16-digit hex kk

  • @Weessmith So I noticed there is no way, only adding characters to the left as in the reply. However I saw in Soen Otherwise, someone trying to convert the 16 digit hexadecimal into date.

2 answers

3


According to the documentation, dechex() supports only integers with values up to 4294967295.

In this case, you need to write a function capable of converting large integers to hexadecimal, see only:

<?php

function bcdechex($dec) {
    $hex = '';
    do {    
        $last = bcmod($dec, 16);
        $hex = dechex($last).$hex;
        $dec = bcdiv(bcsub($dec, $last), 16);
    } while($dec>0);
    return str_pad($hex, 16, "0", STR_PAD_LEFT);
}

$hexstr = bcdechex( time() * 1000 );

echo $hexstr;

?>

Exit:

0000016467d83520
  • Thanks but does not result in a hexadecimal of 16 digits, beyond the function time() return the elapsed time in seconds.

  • @Filipemoraes: I got it, here’s it

  • I have already checked these functions, including the PHP documentation but I need it to be a 16 digit hexadecimal to implement the algorithm correctly. Function is not returning a 16 digit hexadecimal.

  • The function is returned the hexadecimal value correctly. It turns out that the zeros on the left are being deleted.

  • @Filipemoraes: Now it’s ok. Edited answer!

1

You can use the Math/BigInteger to solve your problem and with it create a hexadecimal converter easily:

<?php

include('Math/BigInteger.php');

function toHex($n) {
    $zero = new Math_BigInteger(0);
    if ($n->compare($zero) == 0) return "0";
    $base = "0123456789ABCDEF";
    $dezesseis = new Math_BigInteger(16);
    $result = "";
    while ($n->compare($zero) > 0) {
        list($n, $r) = $n->divide($dezesseis);
        $result = $base[intval($r->toString())] . $result;
    }
    return $result;
}

function milliToHex() {
    $temp = new Math_BigInteger(time());
    $mil = new Math_BigInteger(1000);
    $ret = toHex($temp->multiply($mil));
    return str_pad($ret, 16, "0", STR_PAD_LEFT);
}

echo milliToHex();

?>

For me, it produced this output:

0000016468976E40

See here working on ideone (but don’t be alarmed by the size of the code, as I had to copy and paste the class Math/BigInteger practically whole there in ideone).

I’ve done it too that answer three and a half years ago using some similar concepts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.