haskellhexpretty-printbytestring

Pretty print ByteString to hex nibble-wise


What's an idiomatic way of treating a bytestring nibblewise and pretty printing its hexadecimal (0-F) representation?

putStrLn . show . B.unpack
-- [1,126]

Which, upon further work

putStrLn . show . map (\x -> N.showIntAtBase 16 (DC.intToDigit) x "") . B.unpack
["1","7e"]

But what I really want is

["1","7","e"]

Or better yet

['1','7','e']

I could munge up ["1","7e"] but that string manipulation whereas I'd rather do numeric manipulation. Do I need to drop down to shifting and masking numeric values?


Solution

  • I'd like to elaborate on max taldykin's answer (that I have upvoted), which I think is over-complicated. There is no need for NoMonomorphismRestriction, printf or Data.List.

    Here is my version:

    import qualified Data.ByteString as B
    import Numeric (showHex)
    
    prettyPrint :: B.ByteString -> String
    prettyPrint = concat . map (flip showHex "") . B.unpack
    
    main :: IO ()
    main = putStrLn . prettyPrint . B.pack $ [102, 117, 110]