[Haskell-cafe] Writing binary files

Neil Mitchell ndmitchell at gmail.com
Mon Aug 21 10:35:48 EDT 2006


Hi

> hPutInt h = hPutStr h . map chr . map (0xff .&.)
>                       . take 4 . iterate (`shiftR` 8)
>
> hGetInt h = replicateM 4 (hGetChar h) >>=
>             return . foldr (\i d -> i `shiftL` 8 .|. ord d) 0
>
> This of course assumes that a Char is read/written as a single low-order
> byte without any conversion.  But you'd have to assume a lot more if you
> started messing with pointers.  (Strange, somehow I get the feeling, the
> above is way too easy to be the answer you wanted.)

It's exactly the answer I was hoping for!

Thanks

Neil


More information about the Haskell-Cafe mailing list