Converting things to and from binary

George Russell ger@tzi.de
Tue, 20 May 2003 13:07:24 +0200


Ketil Z. Malde wrote (snipped)
> Would it be possible to separate binary I/O in two layers, the binary
> layer converting to/from (U?)Arrays of Word8, and another, the I/O
> layer, reading and writing such arrays?

I think the proposal I posted is better because it allows you to
mix up both single Word8's and arrays.  In my experience, structures
to be exported typically contain both.  For example, I pack
integers as a sequence of bytes (so that integers between -64 and 63
can be represented by one byte, for example).  Constructing singleton
arrays for each of these bytes would be tiresome, and presumably not
as efficient as writing them directly into the target memory area.

Here are two examples of cases where I want to write binary data to
memory, not to a Handle:
(1) Suppose I maintain a dictionary indexed by Strings (say) whose values
belong to different types with binary representations, and I want this
dictionary too to have a binary representation.  Whenever I look something
up in the dictionary I know what type I'm expecting, but I don't know what
types to expect when decoding the dictionary.  Then the most practical
encoding seems to be as [(String,Bytes)], where each Bytes contains an
encoded entry in the dictionary.
(2) If I want to process binary data in some way before sending it to
the outside world, for example by compressing it using zlib (the API
to the GZIP algorithm), or by encrypting it.