How to convert a ByteString to an Int and dealing with endianness?
Asked Answered
E

3

5

I need to read a binary format in Haskell. The format is fairly simple: four octets indicating the length of the data, followed by the data. The four octets represent an integer in network byte-order.

How can I convert a ByteString of four bytes to an integer? I want a direct cast (in C, that would be *(int*)&data), not a lexicographical conversion. Also, how would I go about endianness? The serialized integer is in network byte-order, but the machine may use a different byte-order.

I tried Googling but that only yold results about lexicographical conversion.

Equisetum answered 15/1, 2013 at 10:29 Comment(0)
E
13

The binary package contains tools to get integer types of various sizes and endianness from ByteStrings.

λ> :set -XOverloadedStrings
λ> import qualified Data.Binary.Get as B
λ> B.runGet B.getWord32be "\STX\SOH\SOH\SOH"
33620225
λ> B.runGet B.getWord32be "\STX\SOH\SOH\SOHtrailing characters are ignored"
33620225
λ> B.runGet B.getWord32be "\STX\SOH\SOH" -- remember to use `catch`:
*** Exception: Data.Binary.Get.runGet at position 0: not enough bytes
CallStack (from HasCallStack):
  error, called at libraries/binary/src/Data/Binary/Get.hs:351:5 in binary-0.8.5.1:Data.Binary.Get
Emblazonry answered 15/1, 2013 at 10:37 Comment(0)
R
6

I assume you can use a fold, and then use either foldl or foldr to determine which endian you want (I forget which is which).

foldl :: (a -> Word8 -> a) -> a -> ByteString -> a

I think this will work for the binary operator:

foo :: Int -> Word8 -> Int
foo prev v = (prev * 256) + v
Ranson answered 15/1, 2013 at 10:35 Comment(0)
P
4

I'd just extract the first four bytes and merge them into a single 32bit integer using the functions in Data.Bits:

import qualified Data.ByteString.Char8 as B
import Data.Char (chr, ord)
import Data.Bits (shift, (.|.))
import Data.Int (Int32)

readInt :: B.ByteString -> Int32
readInt bs = (byte 0 `shift` 24)
             .|. (byte 1 `shift` 16)
             .|. (byte 2 `shift` 8)
             .|. byte 3
        where byte n = fromIntegral $ ord (bs `B.index` n)

sample = B.pack $ map chr [0x01, 0x02, 0x03, 0x04]
main = print $ readInt sample -- prints 16909060
Passionate answered 15/1, 2013 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.