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.
@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?– Wees Smith
@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.
– Filipe Moraes
Boy, I’m racking my brain trying to find a 16-digit hex kk
– Wees Smith
@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.
– Filipe Moraes