How to convert a Integer to a ByteString in Haskell

前端 未结 3 603
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 04:09

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 typ

3条回答
  •  孤独总比滥情好
    2020-12-30 05:01

    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).

提交回复
热议问题