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.
hexdec(string $hex_string): int|float
$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.
Returns the converted decimal integer (if the value exceeds the maximum range of PHP int type, return the float type).
<?php
echo hexdec("1A"); // Output 26
?>
In this example, "1A" is a hexadecimal number, which is converted to decimal and is 26.
<?php
echo hexdec("0xFF"); // Output 255
?>
hexdec() can automatically recognize formats with 0x prefix, so the output is 255.
<?php
echo hexdec("C0FFEE"); // Output 12648430
?>
In this example, C0FFEE is a typical "Egg" hexadecimal string, converted to decimal to 12648430.
In actual development, hexdec() is often used with other functions, such as converting RGB color codes to decimal numbers.
<?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.
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.
<?php
$bigHex = "FFFFFFFF";
echo hexdec($bigHex); // Output 4294967295
?>
<?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.
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)
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.