Jan. 30th, 2010

juan_gandhi: (Default)

-- Reads hex digits from input, writes binaries to output
-- e.g. "CAFEBABE" -> "\xca\xfe\xba\xba\xbe"
import System.IO
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Word

pairwise :: [a] →  [[a]]
pairwise (x:y:rest) = [x,y] : pairwise rest
pairwise _ = []

toByte :: [Char] →  Word8
toByte s = read ("0x" ⊕ s)

convert :: [Char] →  [Word8]
convert = (map toByte)∘pairwise

goodChars :: String →  String
goodChars s = filter (λc →  any (λx →  x ≡ c) "0123456789ABCDEFabcdef") s

toBin = L.pack∘convert∘goodChars∘C.unpack

main = do
  hSetBinaryMode stdout True
  hexes ←  L.getContents
  L.hPut stdout $ toBin hexes

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 2345 6
78 9 10 111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 12th, 2025 09:24 am
Powered by Dreamwidth Studios