Without scrolling in the internal elements of GHC.Integer you can add a bytestring in Integer one byte at a time.
import qualified Data.ByteString as BS import Data.Bits fromBytes :: ByteString -> Integer fromBytes = BS.foldl' f 0 where fab = a `shiftL` 8 .|. fromIntegral b
If you want to read Natural numbers instead of Integer , you can give this a more general type signature.
-- Read bytes in big-endian order (most significant byte first) -- Little-endian order is fromBytes . BS.reverse fromBytes :: (Bits a, Num a) => ByteString -> a fromBytes = BS.foldl' f 0 where fab = a `shiftL` 8 .|. fromIntegral b
source share