link packunpk
May 2, 2001; C. Tenaglia (modified by Richard L. Goerwitz)
This file is in the public domain.
Integers written directly as strings occupy much more space than they need to. One easy way to shrink them a bit is to "pack" them, i.e. convert each decimal digit into a four-byte binary code, and pack these four-bit chunks into eight-bit characters, which can be written to a file. Interestingly, packing decimal strings in this manner lends itself to unpacking by treating each character as a base-10 integer, and then converting it to base-16. Say we have an input string "99." Pack() would convert it to an internal representation of char(16*9 + 9), i.e. char(153). Unpack would treat this char(153) representation as a base-10 integer, and convert it to base 16 (i.e. 10r153 -> 16r99). The 99 is, of course, what we started with. Note that two unpack routines are provided here: The first, by Tanaglia, utilizes convert.icn from the IPL. The second, by Goerwitz, does not. They utilize very different methods, but both amount to basically the same thing. Goerwitz's routine returns an integer, though, and has no "width" argument.