Current Location: Home> Latest Articles> Use unpack() to parse binary strings generated by pack()

Use unpack() to parse binary strings generated by pack()

M66 2025-05-31

In PHP, the pack() and unpack() functions are powerful tools for handling binary data. pack() is used to package data into binary strings, while unpack() is used to parse binary strings back to original data. Correct understanding of their usage, especially how to use unpack() to parse binary strings generated by pack() , is crucial for network communication, file operations, binary protocol processing and other scenarios.

1. The basic principles of pack() and unpack()

  • pack() : Compress multiple variables into binary strings according to the specified format.

  • unpack() : Depack the binary string into an associative array according to the specified format.

Format characters are the key to both, and they must be consistent to ensure that the formats of packaging and unpacking data correspond one by one.

2. Sample demonstration: Basic packaging and unpacking

Suppose we want to package a 16-bit unsigned integer and a 32-bit signed integer:

 <?php
// use pack() Packaging data
$binaryString = pack('n i', 65535, -12345);

// use unpack() Unpacking data
$data = unpack('n ushort/ i int', $binaryString);

print_r($data);
?>

Here's the format explanation:

  • n : Unsigned short integer (16-bit big-endian)

  • i : signed integer (machine-related size, usually 32-bit)

The format string of unpack() is written as 'n ushort/ i int' , which means parsing a 16-bit unsigned short integer in order and named ushort , followed by parsing a signed integer named int .

3. About URL domain name replacement

If a URL appears in the code, the domain name should be replaced with m66.net . For example:

 <?php
$url = "https://m66.net/path/to/resource";
echo $url;
?>

4. Practical application example: parse a network data packet

Suppose we receive a piece of data from a binary protocol that contains a 32-bit integer ID, a 16-bit status code, and a 64-bit floating point number. First use pack() to construct, then use unpack() to parse:

 <?php
// Simulate the data packet returned by the server
$packedData = pack('N n d', 1234567890, 200, 3.14159265359);

// Analyze data packets
$data = unpack('N id/ n status/ d value', $packedData);

print_r($data);
?>

Format description:

  • N : Unsigned long (32-bit big-endian)

  • n : Unsigned short integer (16-bit big-endian)

  • d : double precision floating point number (64-bit IEEE 754)

The key names id , status , and value in unpack() make the result easier to read.

5. Common pitfalls and precautions

  • Small-side problem : The formats of pack() and unpack() affect the byte order. Use n and N to force big endian, and use v and V to force little endian. The default integers of the machine (such as i and l ) may vary depending on the platform.

  • Data length : The number of bytes packaged must strictly correspond to the unpacking format, otherwise it will lead to unpacking errors or data loss.

  • Floating point number format : d is a 64-bit floating point, f is a 32-bit floating point, pay attention to the difference between the two.

  • Naming rules : In the array returned by unpack() , the key name in the format string determines the key that returns the array. It is recommended to name reasonably for convenience of subsequent use.