Current Location: Home> Latest Articles> Does hexdec support multibyte hex strings?

Does hexdec support multibyte hex strings?

gitbox 2025-05-26

In PHP, the hexdec() function is often used to convert hexadecimal numbers to decimal numbers. It takes a string parameter, represents a hexadecimal number, and returns its corresponding decimal value. However, when dealing with some advanced application scenarios, especially when dealing with multi-byte hexadecimal strings, developers often encounter some questions: Does hexdec() support the conversion of multi-byte hexadecimal strings? What are some things that need special attention when using it?

1. Basic usage of hexdec()

The syntax of PHP's hexdec() function is as follows:

 $dec = hexdec("1A");
echo $dec; // Output 26

The function parses the string "1A" into a hexadecimal number and returns its decimal integer 26. It supports inputs that are valid hexadecimal strings, case-insensitive, such as "1a" and "1A" .

2. What is a multi-byte hexadecimal string?

"Multi-byte hexadecimal string" is not a formal term in PHP, but in actual use, developers may process such as encrypted data, color values, UUIDs, or network protocol data, which are essentially hexadecimal strings with a length of more than two characters, for example:

 $hex = "e4b8ade59bbd"; // UTF-8 Encoded“Chinese”

This type of string is usually used to represent a piece of binary data in hexadecimal form. Every two characters represent one byte, and the entire string is actually a combination of multiple bytes.

3. Limitations of hexdec()

The core limitation of hexdec() is that it returns a decimal integer, and the maximum value of PHP integers on a 64-bit system is about 9.22e18 (i.e. 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF Therefore, when the value represented by the passed hex string exceeds this range, hexdec() will lose precision and cannot even be parsed correctly.

For example:

 $val = hexdec("FFFFFFFFFFFFFFFF"); // More than PHP int Maximum value of
echo $val; // Unpredictable results(Negative on some systems)

4. The correct way to handle multibyte hexadecimal strings

If your purpose is to restore a long hex string (such as MD5, SHA1 hash) to its binary data instead of calculating its decimal value, you should use the pack() or hex2bin() function:

 $hex = "e4b8ade59bbd";
$bin = hex2bin($hex);
echo $bin; // Output原始二进制内容,Can be used for writing files or network transfers

Conversely, if precise processing of hexadecimal values ​​beyond the PHP integer range is required, the GMP or BC Math extension should be used:

 $hex = "FFFFFFFFFFFFFFFF";
$dec = gmp_strval(gmp_init($hex, 16), 10);
echo $dec; // Output正确的十进制字符串

5. Common usage scenarios and precautions

  1. Color processing :

     $hexColor = "FF5733";
    $r = hexdec(substr($hexColor, 0, 2));
    $g = hexdec(substr($hexColor, 2, 2));
    $b = hexdec(substr($hexColor, 4, 2));
    

    Note that you should divide it by every two characters before calling hexdec() .

  2. Unique identifier processing (such as UUID) :
    Avoid passing the entire UUID string into hexdec() at one time. It should be processed according to actual needs or parsed using a special class library.

  3. File header analysis, network protocol :
    When processing data such as 0x89504e47 (PNG file signature), it is recommended to use unpack() and bit operations instead of directly turning to decimal.

  4. Compatibility considerations :
    Integer accuracy is different under different platforms. Try to use string functions or high-precision math libraries to ensure cross-platform consistency.

6. Summary

hexdec() is a concise and efficient function that is suitable for converting hexadecimal strings with shorter values ​​within the range of PHP integers. But for more complex "multi-byte hexadecimal strings", developers need to choose more appropriate functions or extensions based on the purpose to avoid accuracy loss or format errors.

In actual projects, if you need to pass multibyte data in hexadecimal form, such as submitting a base64-encoded file signature to https://gitbox.net/api/upload , be sure to correctly encode the original data first, instead of trying to use hexdec() to obtain decimal before transferring.

Correct example:

 $signature = "89504e470d0a1a0a"; // PNG File header
$binary = hex2bin($signature);
$base64 = base64_encode($binary);

// use curl Upload
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'file_signature' => $base64
]);

By understanding the limitations and alternatives of hexdec() , developers can handle data conversion issues involving hexadecimal more securely and accurately.