module Network.RTorrent.Chunk (
convertChunks
, convertChunksPad
) where
convertChunksPad :: Int
-> String
-> Maybe [Bool]
convertChunksPad len = fmap (pad len) . convertChunks
where
pad i str
| i <= 0 = []
| otherwise = case str of
[] -> replicate i False
(x:xs) -> x : pad (i 1) xs
convertChunks :: String
-> Maybe [Bool]
convertChunks [] = Nothing
convertChunks str = fmap concat . mapM convert $ str
where
base2 = reverse . map (toEnum . (`mod` 2)) . take 4 . iterate (`div` 2)
convert = fmap base2 . num
num ch
| '0' <= ch && ch <= '9' = Just $ fromEnum ch fromEnum '0'
| 'A' <= ch && ch <= 'F' = Just $ 10 + fromEnum ch fromEnum 'A'
| otherwise = Nothing