Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: "Reading" ByteString

Tags:

haskell

How do I convert a ByteString representation of an Integer to ... well Integer? Is there a special read function for ByteStrings or do I have to unpack first than use normal read?

Thanks.

like image 987
r.sendecky Avatar asked Mar 23 '12 11:03

r.sendecky


2 Answers

You can use readInt or readInteger from Data.ByteString.Char8. If you want to read some other type of data, you'll need to write your own parser; the best choice is probably attoparsec, which is a library for writing fast ByteString and Text parsers, similar to Parsec.

like image 125
ehird Avatar answered Oct 01 '22 12:10

ehird


In addition to ehird's excellent suggestions, you can also use Data.Binary.Get for reading fixed-length integers.

ghci> :m +Data.Binary.Get
ghci> :t runGet getWord64le
ByteString -> Word64
like image 40
Dan Burton Avatar answered Oct 01 '22 13:10

Dan Burton