Created
August 28, 2012 01:43
-
-
Save mdippery/3494175 to your computer and use it in GitHub Desktop.
Converts a binary representation of a character into a printable character
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.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