In PHP, the hexdec function is a commonly used built-in function that converts hex strings to corresponding decimal values. It is very simple to use:
<?php
$decimal = hexdec("1a3f");
echo $decimal; // Output 6719
?>
However, hexdec has its scope and limitations when dealing with hex strings, and understanding these is essential to avoid errors and data confusion in the program.
hexdec accepts a string parameter that should be a legal hexadecimal expression. This function parses hexadecimal numeric characters (0-9, af, AF) at the beginning of the string until the first non-hexadecimal character stops, returning its corresponding decimal integer. If the string is preceded by 0x or 0X , it will also be recognized:
<?php
echo hexdec("0x1a3f"); // 6719
echo hexdec("1a3fxyz"); // 6719,Subsequent non-hexadecimal characters are ignored
echo hexdec("xyz1a3f"); // 0,Return non-hexadecimal characters at the beginning0
?>
A string composed of pure hexadecimal characters <br> For example: "1a3f", "FF12", "0xABC" (both with or without the 0x prefix), can be converted correctly.
The string must start with hexadecimal characters <br> Hexdec will only convert if the string starts with a valid hexadecimal character.
For example: "abcd123" → Identify the hexadecimal character before "abcd123" until the first non-hexadecimal character stops.
Ignore case <br> The letters in the string can be uppercase or lowercase, and hexdec can be processed correctly.
The string contains non-hexadecimal characters and starts with <br> For example: "g123", "xyz", these strings start with non-hex characters, and hexdec directly returns 0.
Hexadecimal string with spaces or other separators
hexdec will not skip spaces or delimiters. For example, "1a 3f" will stop converting due to encountering spaces and will only convert to the "1a" part.
Very long hexadecimal string
The value returned by hexdec is a floating point number. There will be accuracy loss when dealing with very large hexadecimal numbers. It is recommended to use BCMath or GMP extension to handle large numbers.
<?php
echo hexdec("0x1a3f") . "\n"; // 6719
echo hexdec("1A3F") . "\n"; // 6719
echo hexdec("1a3fxyz") . "\n"; // 6719,Ignore non-hexadecimal tail characters
echo hexdec("xyz1a3f") . "\n"; // 0,Illegal characters at the beginning,Unable to convert
echo hexdec("1a 3f") . "\n"; // 26,Stop encountering space,Turn only1a
?>
The strings suitable for processing that hexdec should be a sequence of continuous characters starting with hexadecimal numbers , which can be prefixed with 0x , but cannot have intermediate spaces or illegal characters.
When non-hexadecimal characters are encountered, parsing will stop, and if non-hexadecimal characters are at the beginning, it will return 0.
For large hexadecimal strings that are outside the numerical range, hexdec may not be able to handle accurately and requires the use of special large number processing functions.
Understanding the above features will help to use the hexdec function to process hex strings more accurately and avoid unexpected errors in data conversion.