
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)

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@haskell.org http://www.haskell.org/mailman/listinfo/libraries

On 11/13/05, Daan Leijen
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
It's now Numeric from the 'base' package: http://haskell.org/ghc/docs/latest/html/libraries/base/Numeric.html Prelude> Numeric.showIntAtBase 2 Char.intToDigit 99 "" "1100011" Prelude> Numeric.showHex 30 "" "1e" -- Friendly, Lemmih

On 11/13/05, Daan Leijen
wrote: 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
It's now Numeric from the 'base' package: http://haskell.org/ghc/docs/latest/html/libraries/base/Numeric.html
Prelude> Numeric.showIntAtBase 2 Char.intToDigit 99 "" "1100011" Prelude> Numeric.showHex 30 "" "1e"
-- Friendly, Lemmih Yes I did try using that (in fact it's in the code I sent) but when you are
On Sunday 13 Nov 2005 2:16 pm, Lemmih wrote: printing out a 1024 bit key, it's not very useful. What I want is something like: 00:01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f 10:11:12:13:14:15:16:17:18:19:1a:1b:1c:1d:1e:1f etc so you can eyeball the octet you are looking for. Should a function like the one I want (and posted) go in NumExts? Dominic.

On 2005-11-13, Dominic Steinitz
On 11/13/05, Daan Leijen
wrote: 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
It's now Numeric from the 'base' package: http://haskell.org/ghc/docs/latest/html/libraries/base/Numeric.html
Prelude> Numeric.showIntAtBase 2 Char.intToDigit 99 "" "1100011" Prelude> Numeric.showHex 30 "" "1e"
-- Friendly, Lemmih Yes I did try using that (in fact it's in the code I sent) but when you are
On Sunday 13 Nov 2005 2:16 pm, Lemmih wrote: printing out a 1024 bit key, it's not very useful. What I want is something like:
00:01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f 10:11:12:13:14:15:16:17:18:19:1a:1b:1c:1d:1e:1f etc
so you can eyeball the octet you are looking for.
Should a function like the one I want (and posted) go in NumExts?
No. There should be one that puts an item in a list every n items, that you can cascade. -- Aaron Denney -><-

On Sun, Nov 13, 2005 at 01:51:22PM +0000, Dominic Steinitz wrote:
I often find myself wanting to print out hex values as a string.
Me too. I have a functions that formats the data in a similar way that the xxd program does.
I couldn't find a library function so I came up with this. Is it worth putting in a library?
Sure.
Thoughts?
I haven't thought about producing a Doc - good idea.
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)
The first guard, (length y == 0), seems a bit expensive - the whole thing will have a quadratic time and can evaluate the input list too eagerly. Best regards Tomasz
participants (5)
-
Aaron Denney
-
Daan Leijen
-
Dominic Steinitz
-
Lemmih
-
Tomasz Zielonka