Hex Values

Dominic Steinitz dominic.steinitz at blueyonder.co.uk
Sun Nov 13 08:51:22 EST 2005


I often find myself wanting to print out hex values as a string. I couldn't 
find a library function so I came up with this. Is it worth putting in a 
library? The obvious place for me would be Data.Codec.Utils.

Thoughts?

Dominic.

hexify :: Integral a => a -> Doc
hexify n =
   let bar = map (map sh) (split 16 (toOctets 256 n))
       foo = map (intersperse colon) (map (map text) bar)
       baz = vcat (map hcat foo)
   in baz

sh x | x < 16    = showHex x "0"
     | otherwise = showHex x ""

split :: Int -> [a] -> [[a]]
split n xs =
   unfoldr (g n) xs where
      g :: Int -> [a] -> Maybe ([a],[a])
      g n y
         | length y == 0 = Nothing
         | otherwise     = Just (splitAt n y)



More information about the Libraries mailing list