Skip to content

Instantly share code, notes, and snippets.

@Lablnet Lablnet/hex2dec.php
Last active Dec 30, 2018

Embed
What would you like to do?
Convert HEXA to decimal
<?php
function hexTDec($hex)
{
$decimal = 0;
$exp = 0;
$len = strlen($hex) - 1;
for ($i = $len; $i >= 0 ; $i--) {
if ($hex[$i] <= 9 && $hex[$i] >= 0){
$decimal += $hex[$i] * ( 16 ** $exp);
}
if ($hex[$i] <= "F" && $hex[$i] >= "A") {
$decimal += (ord($hex[$i]) - 55) * (16 ** $exp);
}
$exp++;
}
return $decimal;
}
//Usage
echo hexTDec("123");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.