module Text.XXD where import qualified Data.ByteString.Base16 as Base16 import Data.ByteString (ByteString) import qualified Data.ByteString as B import Data.Word import Data.Bits import Data.Char import Text.Printf nibble :: Word8 -> Char nibble b = intToDigit (fromIntegral (b .&. 0x0F)) xxd :: Int -> ByteString -> [String] xxd offset bs | B.null bs = [] xxd offset bs = printf "%03x: %s" offset ds : xxd (offset + B.length xs) bs' where ds = unwords $ map (\byte -> [nibble (byte `shiftR` 4), nibble byte]) $ B.unpack xs (xs,bs') = B.splitAt 16 bs {- main = do bs <- B.getContents mapM_ putStrLn $ xxd 0 bs -}