Packing and Unpacking Binary Data
While most of the user information is nicely represented in textual form, other system information is more naturally represented in other forms. For example, the IP address of an interface is internally managed as a four-byte number. While it is frequently decoded into a textual representation consisting of four small integers separated by periods, this encoding and decoding is wasted effort if a human is not interpreting the data in the meantime.
As a result, the network routines in Perl that expect or return an IP
address use a four-byte string that contains one character for each
sequential byte in memory. While constructing and interpreting such a
byte-string is fairly straightforward using
chr
and
ord
(not presented
here), Perl provides a short cut that is equally applicable to more
difficult structures.
The
pack
function works a bit like
sprintf
, taking a
format control string and a list of values, and creating a single
string from those values. The pack
format string
is geared towards creating a binary data structure, however. For
example, to take four small integers and pack them as successive
unsigned bytes in a composite string, use the following format:
$buf = pack("CCCC", 140, 186, 65, 25);
Here, the
pack
format string is
four C
’s. Each C
represents a separate value taken from the following list (similar to
what a %
field does in
sprintf
). The C
format
(according to the Perl manpages, the reference card,
Programming Perl, the HTML files, ...
Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.