Current Location: Home> Latest Articles> How to Use hexdump() to View the Binary Structure Packed by the pack() Function

How to Use hexdump() to View the Binary Structure Packed by the pack() Function

M66 2025-06-12

In PHP, the pack() function is used to pack data into a binary string according to a specified format. It is commonly used in low-level data processing, network communication protocols, file format parsing, and other scenarios. To better understand and debug the binary data packed by pack(), it's often necessary to view its specific byte structure using tools like hexdump.

This article will introduce how to implement a simple hexdump() function in PHP, and how to use it in conjunction with the pack() function to view packed binary data.


Introduction to the pack() Function

The syntax of the pack() function is:

string pack(string $format, mixed ...$values)
  • $format specifies the packing format of the data. For example, 'C' represents an unsigned character (1 byte), 'n' represents a 16-bit unsigned short (network byte order), and 'V' represents a 32-bit unsigned long (little-endian byte order), etc.

  • $values is the list of values to be packed.

For example:

$bin = pack('Cnv', 0x12, 0x3456, 0x789a);

This statement will generate a binary string containing:

  • 1 byte: 0x12

  • 2 bytes (network order, big-endian): 0x3456

  • 2 bytes (little-endian): 0x789a


Custom hexdump() Function

Here is a simple hexdump() function that outputs a binary string in hexadecimal representation by byte, along with the corresponding ASCII characters:

function hexdump(string $data): void {
    $len = strlen($data);
    $offset = 0;
    while ($offset < $len) {
        // Display 16 bytes per line
        $chunk = substr($data, $offset, 16);
        $hex = '';
        $ascii = '';
        $byte = ord($chunk[$i]);
        $hex .= sprintf("%02X ", $byte);
        // Display original character for printable characters, otherwise display a dot
        $ascii .= ($byte >= 32 && $byte <= 126) ? $chunk[$i] : '.';
    }

    // Pad with spaces if the line is less than 16 bytes
    $hex = str_pad($hex, 16 * 3);

    // Output format: offset address + hexadecimal + ASCII
    printf("%08X  %s %s\n", $offset, $hex, $ascii);

    $offset += 16;
}

}


Example: Use hexdump() to View the Result of pack()

Suppose we want to pack some data with the pack() function and view it using hexdump():

<?php
// Packing data: 1-byte unsigned character, 2-byte network order, 4-byte little-endian
$binary = pack('C n V', 0x41, 0x4243, 0x44454647);
<p>hexdump($binary);<br>
?><br>

After execution, the output will be as follows:

00000000  41 42 43 47 46 45 44 00 00 00 00 00 00 00 00 00  ABCGFED..........

Analysis:

  • 0x41 is the character 'A'

  • 0x4243 is stored as 42 43 ('B' 'C') in network byte order.

  • 0x44454647 is stored as 47 46 45 44 ('G' 'F' 'E' 'D') in little-endian order.


Notes

  • The pack() function's format string is very powerful. For detailed format information, please refer to the official documentation, such as m66.net/manual/en/function.pack.php.

  • The implementation of hexdump() can be adjusted according to needs, such as supporting more formats or displaying more annotations.

  • Non-printable characters in binary data cannot be easily understood visually, but with hexdump(), debugging is much easier.


By using the methods described above, developers can visually observe the binary structure of data packed by pack(), which is helpful for debugging and understanding data formats. I hope this article helps you quickly get started with using hexdump() and pack() together.