How to Convert Hexadecimal to Decimal Using PHP?

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

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close