Module pack A library for packing and unpacking binary data.
The library adds two functions to the string library: pack and unpack.

Supported letter codes:

This library was made by Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br) with contributions from Ignacio CastaƱo (castanyo@yahoo.es) and Roberto Ierusalimschy (roberto@inf.puc-rio.br). Original library can be found here.

Usage examples

require"pack" local bindata = string.pack("zf", "foo", 4.2) local mystring,myfloat = string.unpack(bindata, "zf")

Type pack

pack.pack(F, x1) Pack binary data into a string.
pack.unpack(s, F, init) Unpacks a binary string into values.

Type pack

Field(s)

pack.pack(F, x1)

Pack binary data into a string.
The letter codes understood by pack are listed above in module description. Numbers following letter codes indicate repetitions.

Parameters

Return value

a (binary) string containing the values packed as described in F.

Usage:

require"pack"
local bindata = string.pack("zf", "foo", 4.2)

pack.unpack(s, F, init)

Unpacks a binary string into values.
The letters codes are the same as for pack, except that numbers following 'A' are interpreted as the number of characters to read into the string and not as repetitions.
The first value returned by unpack is the next unread position in s, which can be used as the init position in a subsequent call to unpack.
This allows you to unpack values in a loop or in several steps.

Parameters

Return values

  1. the next unread position in s, followed by one value per letter in F until F or s is exhausted.
  2. nil if either s has been exhausted or init is bigger than the length of the s

Usage:

  require"pack"
  local _,mystring,myfloat = string.unpack(bindata, "zf")