Last active
December 30, 2018 04:19
-
-
Save lablnet/a5cac272b91add9d6e4221dc9e38c01c to your computer and use it in GitHub Desktop.
Convert HEXA to decimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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