Search code examples
parsinghaskellbytestring

How do I convert a ByteString to an appropriately sized Word?


Basically I've read in 5 bytes that correspond to a quantity, but I would like to convert it to a Word64. What's the best way to do this?

Edit: I should also say that this is run in an inner loop so performance is critical. Ideally I'd like to do something like:

uint64_t word = 0;
char bytes[5] = getbytes(5)
word += (bytes[0] << 32) 
        + (bytes[1] << 24) 
        + (bytes[2] << 16) 
        + (bytes[3] << 8) 
        + (bytes[4])

Or something similar.


Solution

  • If you just want a simple function, assuming big-endian byte order, this one should do the job:

    foo :: B.ByteString -> Word64
    foo = B.foldl' (\x y -> x * 256 + fromIntegral y) 0
    

    However, if you are reading lots of binary data, you might want to look into using the binary package.