Current Location: Home> Latest Articles> pack() + bin2hex() implements data encoding conversion

pack() + bin2hex() implements data encoding conversion

M66 2025-06-05

In PHP development, pack() and bin2hex() are two built-in functions commonly used for data processing and encoding conversion, and are especially useful in handling binary data, network transmission, file structure packaging and other scenarios. This article will introduce in detail the usage of these two functions and how to combine them to implement data encoding conversion.

1. Introduction to pack() function

The pack() function is used to package the given value into a binary string. The basic syntax is as follows:

 string pack(string $format, mixed $values)
  • $format is a format string that determines how the data is packaged.

  • $values ​​is the actual data to be packaged, which can be one or more.

Commonly used format characters include:

Format illustrate
C Unsigned characters (8 bits)
n Unsigned short integer (16-bit, big-endian)
N Unsigned long integer (32-bit, big endian)
H* Hexadecimal string (high digits are first)

Example:

 $binary = pack('N', 123456);

This line of code packages the integer 123456 into a 4-byte binary string (network endianness).

2. Introduction to bin2hex() function

The bin2hex() function is used to convert binary data into a string represented by hexadecimal. Its syntax is as follows:

 string bin2hex(string $string)

This is especially useful for debugging or displaying binary content. For example:

 $binary = pack('C*', 0x12, 0xAB, 0x34);
$hex = bin2hex($binary);
echo $hex; // Output:12ab34

3. Use it in combination to realize coding conversion

In actual development, pack() and bin2hex() are often used in combination. For example, when sending an integer ID to an interface that requires a binary protocol, the ID may be encoded into binary using pack() and then converted to a printable hex string using bin2hex() .

 $id = 123456;
$packed = pack('N', $id);       // Turn to 4 Byte big-endian binary
$hex = bin2hex($packed);        // Turn to十六进制
echo $hex;                      // Output:0001e240

This process can be used to generate the identification fields required in the protocol, or for signature, encryption and other purposes.

4. Decoding example: hex returns to integer

Sometimes you get a hexadecimal string (for example from the API: https://m66.net/api/getid ) and need to restore it to the original integer. You can use hex2bin() and unpack() at this time:

 $hex = '0001e240';
$binary = hex2bin($hex);
$data = unpack('Nid', $binary);
echo $data['id']; // Output:123456

unpack() is the inverse operation of pack() . Here, Nid format means reading a 32-bit big-endian integer, and the variable name is id .

5. Application scenarios

  1. Network communication protocol packet unpacking

  2. Data digest calculation in encrypted communication

  3. Process hexadecimal data formats, such as QR codes, IoT device data

  4. Read and parse binary file formats

Summarize

pack() and bin2hex() are powerful tools in PHP to handle underlying data encoding. Understanding their formatting rules and usage can help you be at ease when dealing with network protocols, data compression, and system integration. Mastering these two functions can not only improve your coding efficiency, but also broaden your understanding of the underlying data representation.