summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Text/XXD.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/Text/XXD.hs b/src/Text/XXD.hs
new file mode 100644
index 00000000..d835b238
--- /dev/null
+++ b/src/Text/XXD.hs
@@ -0,0 +1,26 @@
1module Text.XXD where
2
3import qualified Data.ByteString.Base16 as Base16
4import Data.ByteString (ByteString)
5import qualified Data.ByteString as B
6import Data.Word
7import Data.Bits
8import Data.Char
9import Text.Printf
10
11nibble :: Word8 -> Char
12nibble b = intToDigit (fromIntegral (b .&. 0x0F))
13
14xxd :: Int -> ByteString -> [String]
15xxd offset bs | B.null bs = []
16xxd offset bs = printf "%03x: %s" offset ds : xxd (offset + B.length xs) bs'
17 where
18 ds = unwords $ map (\byte -> [nibble (byte `shiftR` 4), nibble byte])
19 $ B.unpack xs
20 (xs,bs') = B.splitAt 16 bs
21
22{-
23main = do
24 bs <- B.getContents
25 mapM_ putStrLn $ xxd 0 bs
26 -}