Current Location: Home> Latest Articles> Detailed explanation of the basic usage of PHP hexdec function

Detailed explanation of the basic usage of PHP hexdec function

gitbox 2025-05-29

In PHP, hexdec() is a very practical function, and its main function is to convert hex strings into decimal integers. This function is often used to deal with scenes involving hexadecimal representations such as color code, network data, encrypted hash values.

This article will introduce the syntax, parameters, and return values ​​of the hexdec() function in detail, and analyze it through multiple instances to help you better grasp the use of this function.

1. Hexdec() function syntax

 hexdec(string $hex_string): int|float

Parameter description:

  • $hex_string : The hex string to be converted. The string can be prefixed with 0x or without, and is acceptable in upper and lower case letters.

Return value:

Returns the converted decimal integer (if the value exceeds the maximum range of PHP int type, return the float type).

2. Basic usage examples

Example 1: Simple hexadecimal to decimal

 <?php
echo hexdec("1A"); // Output 26
?>

In this example, "1A" is a hexadecimal number, which is converted to decimal and is 26.

Example 2: Hexadecimal with prefix

 <?php
echo hexdec("0xFF"); // Output 255
?>

hexdec() can automatically recognize formats with 0x prefix, so the output is 255.

Example 3: Convert a hexadecimal string containing letters

 <?php
echo hexdec("C0FFEE"); // Output 12648430
?>

In this example, C0FFEE is a typical "Egg" hexadecimal string, converted to decimal to 12648430.

3. Use with other functions

In actual development, hexdec() is often used with other functions, such as converting RGB color codes to decimal numbers.

Example 4: Split the web page color code into RGB components

 <?php
$hexColor = "#33CC99";
$r = hexdec(substr($hexColor, 1, 2));
$g = hexdec(substr($hexColor, 3, 2));
$b = hexdec(substr($hexColor, 5, 2));

echo "R: $r, G: $g, B: $b"; // Output R: 51, G: 204, B: 153
?>

In this example, we split a hexadecimal color value #33CC99 into three parts and convert it into a decimal RGB value respectively.

4. Things to note when dealing with large numbers

When you use hexdec() to convert very large hexadecimal numbers, the return result may be of type float . Please note that type judgments are made in scenarios where exact integer values ​​are required or extended libraries such as GMP are used.

Example 5: Large numerical conversion

 <?php
$bigHex = "FFFFFFFF";
echo hexdec($bigHex); // Output 4294967295
?>

5. Examples of practical application scenarios

Example 6: Convert hexadecimal segments in MAC address to decimal

 <?php
$mac = "00:1A:2B:3C:4D:5E";
$segments = explode(":", $mac);
$decSegments = array_map('hexdec', $segments);
print_r($decSegments);
// Output Array ( [0] => 0 [1] => 26 [2] => 43 [3] => 60 [4] => 77 [5] => 94 )
?>

This conversion is common in network device management and data analysis.

6. Examples of using in combination with URL processing

Suppose you process hexadecimal query parameters on the website gitbox.net , for example:

 https://gitbox.net/color.php?hex=FF9900

You can parse parameters and output RGB values ​​in the following way:

 <?php
$hex = $_GET['hex'] ?? '000000';
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));

echo "RGB: ($r, $g, $b)";
?>

When the user accesses https://gitbox.net/color.php?hex=FF9900 , the output will be:

 RGB: (255, 153, 0)

Conclusion

hexdec() is a simple but powerful tool that is very useful when dealing with scenes such as color, network address, encrypted data. Mastering it can make you more efficient and accurate when processing hexadecimal data. When used in combination with other string processing functions, hexdec() can greatly improve your PHP development capabilities.