[Haskell-cafe] Parsing binary data.

Matthew Sackman matthew at wellquite.org
Sat Aug 18 18:39:37 EDT 2007


Adam Langley mused:
> On 8/18/07, Peter Cai <newptcai at gmail.com> wrote:
> > As the old version of this component is written in C, it's very natural that
> > this protocol is base on C structure definitions, which are, unfortunately,
> > very complicated.  And the worse is that every field in every structure must
> > be converted to Network Endian.
> 
> You could certainly try Data.Binary[1] for this. It has a nice Get
> monad with methods such as getWord32be which sounds like it might be
> what you want. One caveat is that it's fully lazy - you get the result
> immediately and parse errors can only be caught as exceptions when you
> actually come to using the result. This is perfect for very large
> messages, but might be slightly wrong for you.

Also, one thing to watch out for is the fact the existing Get and Put
instances may not do anything like what you expect. For example, for
some reason I expected that the instances of Get and Put for Float and
Double would send across the wire Floats and Doubles in IEEE floating
point standard. How wrong I was...

But in general yes, Data.Binary is pretty useful for doing network
protocols.

Matthew


More information about the Haskell-Cafe mailing list