How to convert a Integer to a ByteString in Haskell
Asked Answered
C

3

19

We'd like to serialize data in a specific binary format. We use Data.ByteStrings internally.

So, the question is: How to convert the different data types we use to a ByteString. For String we have no problem, we can use encodeLazyByteString UTF8 "string". But we'd also like to convert Integers to ByteStrings (big-endian).

Does anyone know how to do that and/or has any good tips using Haskell and binary formats?

Thanks!

Conard answered 17/2, 2010 at 17:56 Comment(0)
P
18

A perfect job for Data.Binary:

Prelude> :m + Data.Binary
Prelude Data.Binary> encode (pi :: Double)
Chunk "\SOH\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\a\CAN-DT\251!\EM\255\255\255\255\255\255\255\205" Empty

Prelude Data.Binary> encode (42 :: Integer)
Chunk "\NUL\NUL\NUL\NUL*" Empty

to yield lazy bytestrings, which can of course be converted to strict ones. The cereal package provides much the same interface, but yields strict bytestrings only (so no infinite streaming of encodings).

Piccoloist answered 17/2, 2010 at 20:35 Comment(2)
Why there are some "\NUL" padding ahead? Is it possible to remove them?Coimbra
"Padding" is included to encode it to a fixed number of bytes. Try encode (42 :: Int8) and you will see no padding, for example.Hallucinate
M
5

For those, like me, looking for a function to convert an Int or Integer to a ByteString you can use: Data.ByteString.Char8.pack . show Even better if it compiles in your ghc you can use show from TextShow. I understand that this is not quite the OP was asking but people looking for the preceding may end up puzzled at this page due to its title.

Margherita answered 16/2, 2019 at 0:25 Comment(1)
But if you have a lot, I think stuff like word16Dec :: Word16 -> Builder will perform better. Since you can concat these builders faster than you can concat bytestrings.Stahl
C
3

Have a look at the binary package, or any of its non-lazy variants: cereal or binary-strict .

In all three cases, since you have a specific binary format, I'd ignore the type class Binary defined in each, and instead focus on the Put and Get monads they define.

Caput answered 17/2, 2010 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.