Skip to content

Instantly share code, notes, and snippets.

@lablnet
Last active December 30, 2018 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lablnet/a5cac272b91add9d6e4221dc9e38c01c to your computer and use it in GitHub Desktop.
Save lablnet/a5cac272b91add9d6e4221dc9e38c01c to your computer and use it in GitHub Desktop.
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