Created
October 16, 2010 01:01
-
-
Save pib/629245 to your computer and use it in GitHub Desktop.
hexdump in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Char | |
import System.IO | |
import Text.Printf | |
main = do | |
hSetBinaryMode stdin True | |
contents <- getContents | |
putStr (hexdump contents) | |
hexdump :: String -> String | |
hexdump contents = | |
let chunks = chunkify 8 contents | |
hexchunks = map hexify chunks | |
pchunks = map (\chunk -> map (\c -> if (isAscii c && c /= '\n') then c else '.') chunk) chunks | |
dump = map (\(h, p) -> printf "%-24s %s" h p) (zip hexchunks pchunks) | |
in unlines dump | |
chunkify :: Int -> [a] -> [[a]] | |
chunkify _ [] = [] | |
chunkify chunksize l = chunk : chunkify chunksize rest | |
where (chunk, rest) = splitAt chunksize l | |
hexify :: String -> String | |
hexify = unwords . map (printf "%02x" . ord :: Char -> String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment