Skip to content

Instantly share code, notes, and snippets.

@mdippery
Created August 28, 2012 01:43
Show Gist options
  • Save mdippery/3494175 to your computer and use it in GitHub Desktop.
Save mdippery/3494175 to your computer and use it in GitHub Desktop.
Converts a binary representation of a character into a printable character
import Data.Bits (shift)
import Data.Char (chr)
strip = takeWhile (\ch -> ch /= '\n')
removeSpaces = filter (\ch -> ch == '0' || ch == '1')
chrToInt '0' = 0
chrToInt '1' = 1
binChrToInt = map chrToInt
tupleToVal (_,0) = 0
tupleToVal (i,v) = shift v (7 - i)
enumerate ls = zip [0..length ls] ls
binStrToInt :: String -> Int
binStrToInt s = sum $ map tupleToVal $ enumerate $ binChrToInt s
binToChr s = chr $ binStrToInt s
main = do
input <- getContents
let binStr = removeSpaces $ strip input
let output = binToChr binStr
putChar output
putChar '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment