
Click above to get retro games delivered to your door ever month!
X-Hacker.org- PERL 4.0 Reference Guide - Norton Guide
[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
pack(TEMPLATE,LIST)
Takes an array or list of values and packs it into a
binary structure, returning the string containing
the structure. The TEMPLATE is a sequence of char-
acters that give the order and type of values, as
follows:
A An ascii string, will be space padded.
a An ascii string, will be null padded.
c A signed char value.
C An unsigned char value.
s A signed short value.
S An unsigned short value.
i A signed integer value.
I An unsigned integer value.
l A signed long value.
L An unsigned long value.
n A short in "network" order.
N A long in "network" order.
f A single-precision float in the native format.
d A double-precision float in the native format.
p A pointer to a string.
x A null byte.
X Back up a byte.
@ Null fill to absolute position.
u A uuencoded string.
b A bit string (ascending bit order, like vec()).
B A bit string (descending bit order).
h A hex string (low nybble first).
H A hex string (high nybble first).
Each letter may optionally be followed by a number
which gives a repeat count. With all types except
"a", "A", "b", "B", "h" and "H", the pack function
will gobble up that many values from the LIST. A *
for the repeat count means to use however many items
are left. The "a" and "A" types gobble just one
value, but pack it as a string of length count, pad-
ding with nulls or spaces as necessary. (When
unpacking, "A" strips trailing spaces and nulls, but
"a" does not.) Likewise, the "b" and "B" fields
pack a string that many bits long. The "h" and "H"
fields pack a string that many nybbles long. Real
numbers (floats and doubles) are in the native
machine format only; due to the multiplicity of
floating formats around, and the lack of a standard
"network" representation, no facility for inter-
change has been made. This means that packed float-
ing point data written on one machine may not be
readable on another - even if both use IEEE floating
point arithmetic (as the endian-ness of the memory
representation is not part of the IEEE spec). Note
that perl uses doubles internally for all numeric
calculation, and converting from double -> float ->
double will lose precision (i.e. unpack("f",
pack("f", $foo)) will not in general equal $foo).
Examples:
$foo = pack("cccc",65,66,67,68);
# foo eq "ABCD"
$foo = pack("c4",65,66,67,68);
# same thing
$foo = pack("ccxxcc",65,66,67,68);
# foo eq "AB\0\0CD"
$foo = pack("s2",1,2);
# "\1\0\2\0" on little-endian
# "\0\1\0\2" on big-endian
$foo = pack("a4","abcd","x","y","z");
# "abcd"
$foo = pack("aaaa","abcd","x","y","z");
# "axyz"
$foo = pack("a14","abcdefg");
# "abcdefg\0\0\0\0\0\0\0"
$foo = pack("i9pl", gmtime);
# a real struct tm (on my system anyway)
sub bintodec {
unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
The same template may generally also be used in the
unpack function.
See Also: unpack
Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson