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?
You can now use Data.ByteString.Builder
. To print a ByteString
to its hex equivalent (with two hex digits per byte, in the right order, and efficiently), simply use:
toLazyByteString . byteStringHex
or
toLazyByteString . lazyByteStringHex
depending on which flavor of ByteString
you have as input.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With