Converting things to and from binary
Derek Elkins
ddarius@hotpop.com
Tue, 20 May 2003 05:46:53 -0400
On 20 May 2003 09:07:24 +0200
ketil@ii.uib.no (Ketil Z. Malde) wrote:
> Glynn Clements <glynn.clements@virgin.net> writes:
>
> > George Russell wrote:
>
> >> The general problem to solve is "How to we convert things to and
> >> from a binary format, so they can be efficiently written to and
> >> from disk". I have solved this (incompletely) twice myself,
>
> > Possibly the best way to handle this would be to extend the Storable
> > class, e.g.
>
> > class Storable a where
> :
> [snip] :
> > toByteArray :: a -> Array Int Word8
> > fromByteArray :: Array Int Word8 -> a
>
> I wonder if this could be used to address a question (or perhaps
> "frustration" is a better term), namely that of efficient packing of
> data structures in memory.
>
> 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?
>
> Something almost entirely, but not quite, unlike:
>
> class Binary a where
> toBin :: a -> Array Int Word8
> toBinList :: [a] -> Array Int Word8
> fromBin :: Array Int Word8 -> a -- perhaps with
> offset? fromBinList :: Array Int Word8 -> [a]
>
> hPutArray :: Handle -> Array Int Word8 -> IO ()
> hGetArray :: ...
> writeBinaryFile :: FilePath -> Array Int Word8 -> IO ()
> :
>
> -kzm
What's wrong with NHC's Binary/BinArray library? There seems to be a
GHC port on the GreenCard page.