You can use the hexdec()
function:
$timeStamp = hexdec('531cb6d6'); // 1394390742
If you want to sanitize the string before using it then you can create your own filter using a callback:
$hexTimestamp = filter_input(INPUT_GET, 'azk', FILTER_CALLBACK, array(
'options' => function($str) {
return ctype_xdigit($str) ? $str : 0;
}
));
$timeStamp = hexdec('531cb6d6');
If you not going to use the HEX value then you could also convert it to an integer as part of the sanitizing:
$timestamp = filter_input(INPUT_GET, 'azk', FILTER_CALLBACK, array(
'options' => function($str) {
return ctype_xdigit($str) ? hexdec($str) : false;
}
));
Edit:
Okay, I am getting confused, I think. You have a HEX input and you want to turn it into a "HEX number". I am not sure I understand what you mean.
If you want the input HEX value to become a "real" PHP HEX value, like 0x531cb6d6
, then you are mistaken about how PHP works. When PHP sees a HEX constant, like 0x531cb6d6
, then it immediately converts it to an integer. PHP does not use HEX numbers; HEX notation is just a way of representing a number, and it not actually a type of number distinct from integers.
If you want the input HEX value, "531cb6d6"
, to look like a HEX value with the 0x
prefix then you can just add it: '0x'.$timeStamp
.
Not sure if I answered your question.