pack.c: Functions to pack and unpack binary data

link cfunc
November 17, 1997; Gregg M. Townsend
Requires: Dynamic loading
This file is in the public domain.

s := pack(value, flags, width)
x := unpack(string, flags)

Flag characters are as follows:

   l -- little-endian [default]
   b -- big-endian
   n -- host platform's native packing order

   i -- integer [default]
   u -- unsigned integer
   r -- real (host platform's native float or double format)

The default width is 4.

Integer values must fit in a standard Icon integer (not large integer).
Consequently, a word-sized value cannot have the high bit set if unsigned.
Floating values can only be converted to/from a string width matching
sizeof(float) or sizeof(double).

Size/type combinations that can't be handled produce errors.
Valid combinations produce failure if the value overflows.

Some of this code assumes a twos-complement architecture with 8-bit bytes.

Source code | Program Library Page | Icon Home Page