[Haskell-cafe] Writing binary files?

Glynn Clements glynn.clements at virgin.net
Sat Sep 11 12:12:53 EDT 2004

Ron de Bruijn wrote:

> I would like to write and read binary files in
> Haskell. I saw the System.IO module, but you need a
> (Ptr a) value for using that, and I don't need
> positions. I only want to read a complete binary file
> and write another binary file. 

You just need to open the files with System.IO.openBinaryFile instead
of openFile (files opened with the latter will have automatic LF/CRLF
translation on Windows).

Converting between Chars and octets (i.e. Word8) is just a matter of:

	import Char (ord, chr)

	charToOctet :: Char -> Word8
	charToOctet = fromIntegral . ord

	octetToChar :: Word8 -> Char
	octetToChar = chr . fromIntegral

Glynn Clements <glynn.clements at virgin.net>

More information about the Haskell-Cafe mailing list