Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

pack

Converts a list of values into a binary string according to a template.

Takes a TEMPLATE string and a LIST of values and packs them into a binary string. The template is a sequence of format characters, each optionally followed by a repeat count or * (consume all remaining).

Common format characters:

  • a, A, Z – ASCII string (null-padded, space-padded, null-terminated)
  • c, C – signed/unsigned char (8-bit)
  • s, S – signed/unsigned short (16-bit native)
  • l, L – signed/unsigned long (32-bit native)
  • q, Q – signed/unsigned quad (64-bit native)
  • n, N – unsigned short/long in network (big-endian) byte order
  • v, V – unsigned short/long in VAX (little-endian) byte order
  • f, d – single/double-precision float (native)
  • H, h – hex string (high/low nybble first)
  • w – BER compressed integer
  • U – Unicode code point
  • x – null byte padding
  • X – back up one byte
  • @ – null-pad to absolute position
  • (...)N – group with repeat count

Whitespace and #-comments in templates are ignored.

Synopsis

my $bin = pack("A10", "hello");         # space-padded string
my $bin = pack("NnA*", $long, $short, $str);
my $bin = pack("(A2)*", @pairs);        # group repeat

See Also

unpack, vec