Skip to content

Instantly share code, notes, and snippets.

@monoid
Created August 1, 2014 16:34
Show Gist options
  • Select an option

  • Save monoid/d7d8574760e0521701d1 to your computer and use it in GitHub Desktop.

Select an option

Save monoid/d7d8574760e0521701d1 to your computer and use it in GitHub Desktop.
ByteString: hex integers
module HexBs where
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Char
itox :: Integer -> B.ByteString
itox 0 = B.singleton '0'
itox n | n > 0 = B.reverse $ B.unfoldr unhex n
where unhex i = if i == 0 then
Nothing
else
Just (intToDigit $ fromIntegral b, a)
where (a, b) = divMod i 16
xtoi :: B.ByteString -> Integer
xtoi bs = B.foldl' hex 0 bs
where hex i c = i*16 + (toInteger $ digitToInt c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment