Created
August 1, 2014 16:34
-
-
Save monoid/d7d8574760e0521701d1 to your computer and use it in GitHub Desktop.
ByteString: hex integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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