I'm working on a programming assignment: converting hex to decimal and vice versa. I know there's built in functions for this, but I'm supposed to use repeated division and multiplication to achieve converting from one base to another.
Anyway, I don't need help with the overall project. I just have a few helper functions I'm trying to put together, one of which is a hex digit to integer converter. Not very elegant, but it works....kinda.
//function hex2int() takes a hexadecimal digit and returns an int
function hex2int($var) {
if ($var == "0") {$final = 0;}
else if ($var == "1") {$final = 1;}
else if ($var == "2") {$final = 2;}
else if ($var == "3") {$final = 3;}
else if ($var == "4") {$final = 4;}
else if ($var == "5") {$final = 5;}
else if ($var == "6") {$final = 6;}
else if ($var == "7") {$final = 7;}
else if ($var == "8") {$final = 8;}
else if ($var == "9") {$final = 9;}
else if (($var == "a") || ($var == "A")) {$final = 10;}
else if (($var == "b") || ($var == "B")) {$final = 11;}
else if (($var == "c") || ($var == "C")) {$final = 12;}
else if (($var == "d") || ($var == "D")) {$final = 13;}
else if (($var == "e") || ($var == "E")) {$final = 14;}
else if (($var == "f") || ($var == "F")) {$final = 15;}
else echo "Error: a single hexadecimal digit is required.";
return $final;
}
I then echo $final to the browser. It works great, but 0 doesn't echo anything! What's going on here?