[Haskell-cafe] Re: Parsing binary data.

Aaron Denney wnoise at ofb.net
Mon Aug 20 18:03:52 EDT 2007

On 2007-08-19, Matthew Sackman <matthew at wellquite.org> wrote:
> Recently, Adam Langley responded so:
>> 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).
> Gah, that'll teach me to post from memory without checking the code.
> Indeed, that is what I meant, the instances of Binary.
>> 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.
> Quite. Whilst we're on the subject (and I realise I might be hijacking
> this thread a little), it does seem rather odd that it's very easy to
> take a Word8/16/32/64 and interpret it as an integer. Similarly, it's
> very easy to take an integer and convert it to a Word of some sort.

That's because there's basically only one way to interpret a given word
as an integer, and store a given integer as a word.

> But it's vastly harder to do that for floats / non-integers. Now I know
> that the number classes in the Prelude are basically broken anyway and
> all really need rewriting, but it does seem completely arbitrary that
> Words somehow are only allowed to contain whole numbers!

It's more that for floats, there are a zillion plausible ways to store
them, and many have been used.

Aaron Denney

More information about the Haskell-Cafe mailing list