0

As a haskell newbie i stuck with the problem: I have a binary data in bytestring and i want to convert it to the list of Word128's (assume that binary data is 16 byte aligned). Actually I have troubles even to convert 16 byte bytestring to the Word128.

Anybody can help ? Thanks!

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
Aleksander Kois
  • 319
  • 1
  • 3
  • 7

1 Answers1

1

Usually you'll write an instance for the binary parsing library of your choice. E.g. for Data.Binary, you'd write something like:

-- | Read a Word64 in big endian format
getWord64be :: Get Word64
getWord64be = do
    s <- readN 8 id
    return $! (fromIntegral (s `B.index` 0) `shiftl_w64` 56) .|.
              (fromIntegral (s `B.index` 1) `shiftl_w64` 48) .|.
              (fromIntegral (s `B.index` 2) `shiftl_w64` 40) .|.
              (fromIntegral (s `B.index` 3) `shiftl_w64` 32) .|.
              (fromIntegral (s `B.index` 4) `shiftl_w64` 24) .|.
              (fromIntegral (s `B.index` 5) `shiftl_w64` 16) .|.
              (fromIntegral (s `B.index` 6) `shiftl_w64`  8) .|.
              (fromIntegral (s `B.index` 7) )
{- INLINE getWord64be -}

that would then be glued into an instance Binary.

So, pick your binary parsing library (either binary or cereal), and write an instance for your type.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468