Sai A Sai A
Updated date Jul 31, 2023
In this blog, we will explore various methods to convert hexadecimal numbers to decimal in PHP with this comprehensive blog. Learn how to use PHP's built-in hexdec() function, perform manual conversions, utilize base_convert() for concise results, and combine sscanf() and sprintf() functions for alternative approaches.
  • 14.4k
  • 0
  • 0

Introduction:

Hexadecimal is base-16, while decimal is base-10. Converting hexadecimal numbers to decimal is a common task in programming, especially when dealing with low-level programming, color representations, and data manipulation. In this blog, we will explore multiple methods to convert hexadecimal numbers to decimals in PHP

Method 1: Using the PHP built-in function hexdec()

PHP provides a built-in function called hexdec() that directly converts a hexadecimal string to its decimal representation. This function simplifies the conversion process and saves us from writing additional code.

$hexValue = '1A3';
$decimalValue = hexdec($hexValue);
echo "Hexadecimal: $hexValue\n";
echo "Decimal: $decimalValue\n";

Output:

Hexadecimal: 1A3
Decimal: 419

In this method, we use the hexdec() function, which takes a hexadecimal string as input and returns its decimal equivalent. The function automatically handles the conversion and produces the desired output.

Method 2: Manual Conversion

If you want to understand the underlying process of converting hexadecimal to decimal, you can perform the conversion manually. This approach involves iterating through each digit of the hexadecimal number and calculating its decimal value based on its position.

$hexValue = '1A3';
$decimalValue = 0;
$length = strlen($hexValue);

for ($i = 0; $i < $length; $i++) {
    $digit = $hexValue[$i];
    $decimalValue += hexDigitToDecimal($digit) * pow(16, ($length - $i - 1));
}

function hexDigitToDecimal($digit) {
    return ctype_digit($digit) ? (int)$digit : ord(strtoupper($digit)) - 55;
}

echo "Hexadecimal: $hexValue\n";
echo "Decimal: $decimalValue\n";

Output:

Hexadecimal: 1A3
Decimal: 419

In this method, we manually convert each digit of the hexadecimal number to its decimal equivalent. We use the function hexDigitToDecimal() to perform the conversion for each digit. If the digit is a number, we simply convert it to an integer; otherwise, we convert it to a decimal value using the ASCII value of the uppercase character minus 55.

Method 3: Using base_convert() function

Another approach to convert hexadecimal to decimal is by using the base_convert() function. This function allows you to convert numbers between different bases, including hexadecimal and decimal.

$hexValue = '1A3';
$decimalValue = base_convert($hexValue, 16, 10);
echo "Hexadecimal: $hexValue\n";
echo "Decimal: $decimalValue\n";

Output:

Hexadecimal: 1A3
Decimal: 419

In this method, we use the base_convert() function, which takes three arguments - the input number, the source base (in this case, hexadecimal, represented by 16), and the target base (decimal, represented by 10). The function automatically performs the conversion and returns the result.

Method 4: Using sscanf() and sprintf() functions

In this method, we use the combination of sscanf() and sprintf() functions to convert hexadecimal to decimal. sscanf() is used to parse the hexadecimal value, and sprintf() is used to format the decimal output.

$hexValue = '1A3';
$decimalValue = sscanf($hexValue, "%x");
$decimalString = sprintf("%d", $decimalValue);
echo "Hexadecimal: $hexValue\n";
echo "Decimal: $decimalString\n";

Output:

Hexadecimal: 1A3
Decimal: 419

In this method, we use sscanf() with the "%x" format to parse the hexadecimal string and get its decimal value. Then, we use sprintf() with the "%d" format to convert the decimal value back to a string, which is the desired output.

Conclusion:

This blog explored various methods to convert hexadecimal numbers to decimals in PHP. We started by using the built-in function hexdec(), which provides a straightforward solution for conversion. Next, we explored the manual conversion process, where we iteratively converted each digit to its decimal value. Then, we used the base_convert() function, which offered a concise way to handle the conversion. Lastly, we combined sscanf() and sprintf() functions for an alternative approach.

Comments (0)

There are no comments. Be the first to comment!!!