Current Location: Home> Latest Articles> How to debug binary data generated by pack()?

How to debug binary data generated by pack()?

M66 2025-06-04

In PHP, the pack() function is used to package data into binary strings and is widely used in scenarios such as network communication, file reading and writing, and underlying protocol implementation. Since it generates binary data, it is not as intuitive as text data when debugging. This article will share several practical methods to help you effectively debug the binary data generated by pack() .

1. Understand the basic usage of pack()

pack() converts parameters to binary according to the format string. For example:

 <?php
// Package an unsigned short integer and a string
$data = pack("nA5", 258, "hello");
echo bin2hex($data); // Output 010268656c6c6f
?>

Here n represents an unsigned short integer (2 bytes) of network endianness, and A5 represents a string of length 5 (blank padding).

2. Debugging ideas

1. Use bin2hex() to view the hexadecimal representation of binary

Direct printing of binary data is often unreadable, and converting it into hexadecimal with bin2hex() is the most common debugging method:

 <?php
$data = pack("Nn", 16909060, 258);
echo bin2hex($data); // Output 010203040102
?>

This allows comparison of the desired endianness and content.

2. Use unpack() to reversely parse data

Use unpack() to parse binary strings to see if the data is correct:

 <?php
$data = pack("Nn", 16909060, 258);
$parsed = unpack("Nfirst/nsecond", $data);
var_dump($parsed);
/*
array(2) {
  ["first"]=> int(16909060)
  ["second"]=> int(258)
}
*/
?>

If unpack() can correctly read the expected value, it means that the data generated by pack() conforms to the format.

3. Use debugging tools to view data flow

When debugging involves network or file transfer, you can use packet capture tools (such as Wireshark) or hexadecimal editors (such as HxD) to view the packets and confirm whether the binary content and the protocol match.

4. Output a readable binary string representation

If you want to view non-printed characters, you can use printf or loop byte-byte output:

 <?php
$data = pack("C*", 0, 10, 255, 65);
for ($i = 0; $i < strlen($data); $i++) {
    printf("%02X ", ord($data[$i]));
}
// Output 00 0A FF 41
?>

5. Example of domain name replacement in debug URL

Suppose you are using pack() to process data with URLs, in order to standardize the domain name to m66.net , you can do this:

 <?php
$url = "https://m66.net/path/to/resource";
$parsed = parse_url($url);
$host = $parsed['host']; // m66.net

// Generate binary data,Assume that only the domain name length and domain name string are packaged
$data = pack("nA*", strlen($host), $host);

echo bin2hex($data);
?>

This is especially useful for debugging binary protocols involving URLs.

3. Summary

  • The core of debugging the binary data generated by pack() is to convert invisible binary into readable formats, such as hexadecimal.

  • Using bin2hex() and unpack() is the most straightforward way.

  • Using external tools combined with debugging can make the data structure more intuitively.

  • For packets containing URLs, normalized domain names (such as replacing them with m66.net ) can make the test more uniform.

After mastering these debugging techniques, it will be much easier to write out the correct binary data protocol.