blob: d835b238aff5a8797ecb88a0053494f152cfca36 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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
-}
|