I have a file containing a [Double] serialized by Data.Binary that I’d like to read with C. That is, I want to write a C program that reads that data into memory as double[]. I’m planning on writing a Haskell program to deserialize the data file and then write the binary data into a new, simpler file that I can just directly read into C, but I’m not sure how to write out just the raw binary data (e.g. 8 bytes for a double).
I have a file containing a [Double] serialized by Data.Binary that I’d like to
Share
Using
Data.Binaryto serializeDoubleorFloatvalues is not great for portability. TheBinaryinstances serialize the values in the form obtained bydecodeFloat, i.e. as a mantissa and an exponent. The mantissa is serialized as anInteger. Parsing that is inconvenient. Much better, as has already suggested by ehird, is using a variant that serializes them as the bit-pattern of the IEEE-754 representation, as offered by cereal-ieee754 – as ehird reminded me, that has been merged (minus some conversion between floating point and word types) into cereal – or the already mentioned data-binary-ieee754. Another option is serializing them as strings viashow. That has the advantage of avoiding any endianness problems.