Hex Values

Daan Leijen daan at cs.uu.nl
Sun Nov 13 09:11:10 EST 2005


Dominic Steinitz wrote:
> I often find myself wanting to print out hex values as a string. I couldn't 

Me too! And I also often want to see binary values (ie. 13 == 0x0D == 0b1101)

Just some information: I remember that there is a 'showHex' function in the "NumExts"
module in Ghc (at least in version 5.x). See:

<http://www.haskell.org/ghc/docs/latest/html/hslibs/sec-NumExts.html>

All the best,
-- Daan.

> 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)
> 
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries



More information about the Libraries mailing list