Packing and Unpacking Binary Data


The pack vocabulary implements words for converting between byte buffers and Factor values. It supports reading and writing various numeric formats, and writing null-terminated strings.

The serialized format to convert to and from is defined by a string of characters, one per field, with different characters denoting different field types. In cases where there are different signed and unsigned variants for a type, a lower-case letter denotes a signed field and an upper-case letter an unsigned one; in cases where there is no distinction, either case can be used interchangeably. For example, a format string of "sCCID" denotes a format consisting of a signed 16-bit integer, followed by two unsigned 8-bit integers, one unsigned 32-bit integer, and one double-precision floating point value.

The complete list of supported field types:
C/cUnsigned/signed 8-bit int (char)
S/sUnsigned/signed 16-bit int (short)
T/tUnsigned/signed 24-bit int
I/iUnsigned/signed 32-bit int (long)
Q/qUnsigned/signed 64-bit int (long long)
F/fSingle-precision (32-bit) IEEE floating point
D/dDouble-precision (64-bit) IEEE floating point
aNull terminated string (write only)


Words for packing to byte sequences:
pack-native ( seq str -- bytes )

pack-be ( seq str -- bytes )

pack-le ( seq str -- bytes )


Words for unpacking from byte sequences:
unpack-native ( bytes str -- seq )

unpack-be ( bytes str -- seq )

unpack-le ( bytes str -- seq )


Words for unpacking from an input stream:
read-packed-native ( str -- seq )

read-packed-be ( str -- seq )

read-packed-le ( str -- seq )