summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-09-15 03:40:40 -0400
committerjoe <joe@jerkface.net>2017-09-15 03:40:40 -0400
commit3ceede524fd4956170b592d6b73faa159c43c5d7 (patch)
tree5876c6775101d251fa638bf7654e91d5d6bae447 /src
parent2d26a10be45a377cd76346336de9667b278a2c28 (diff)
Added XXD module, useful for debuging bytestings.
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 -}