In programming, you may come across situations where you need to convert hexadecimal numbers to decimal numbers. Hexadecimal numbers are base-16 numbers, while decimal numbers are base-10 numbers. Converting a hexadecimal number to a decimal number involves performing some calculations.
// Define hexadecimal number as a string
$hex = '1A';
// Convert hexadecimal number to decimal
$decimal = hexdec($hex);
// Print the decimal equivalent of the hexadecimal number
echo $decimal; // Output: 26
In PHP, it is easy to convert Hexadecimal to Decimal. PHP provides hexdec()
function which returns the decimal equivalent of the hexadecimal number represented by the hex_string argument.
echo hexdec("ab");
// 171
This method will ignore any non-hexadecimal characters in the provided string.
echo hexdec("abs");
// 171. It skips "s" in the string.
hexdec() converts numbers to an integer type. If the number is too large to fit in an integer type, it is converted to float one.
echo hexdec("AAA3453AADBBCCFFEE");
//3.1477113666501E+21