[Haskell-cafe] Parsing binary data.

Adam Langley agl at imperialviolet.org
Sun Aug 19 12:08:12 EDT 2007


On 8/18/07, Matthew Sackman <matthew at wellquite.org> wrote:
> 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...

Ah, those aren't instances of Get and Put, but of Binary[1]. You use
the Binary instances via the functions 'get' and 'put' (case is
important).

Get and Put provide actions like "putWord32be", for which the
resulting bits are pretty much universally accepted. Binary has
default instances which uses Get and Put to serialise Haskell types
like [Int], or (Float, Float). Here the resulting bits aren't
documented, but you can read the code and I have some C code for
dealing with them somewhere if anyone is interrested. The
serialisation of Float is, indeed, nothing like IEEE in either
endianness.

(* and, although Get isn't currently a class, I have sent patches to
dons to make it so, with a default instance which matches current
behaviour and speed, and an alternative which returns a Maybe,
removing a little bit of lazyness in cases where you want to handle
parse failures in pure code. Hopefully something will happen with this
at the next sprint ;) )

[1] http://www.cse.unsw.edu.au/~dons/binary/Data-Binary.html#1

-- 
Adam Langley                                      agl at imperialviolet.org
http://www.imperialviolet.org                       650-283-9641


More information about the Haskell-Cafe mailing list