Reading/Writing Binary Data in Haskell

Wolfgang Jeltsch wolfgang@jeltsch.net
Wed, 9 Jul 2003 10:21:17 +0200


On Wednesday, 2003-07-09, 05:31, Glynn Clements wrote:
> [...]

> There isn't a standard mechanism for binary I/O.

NHC98 contains the York Binary library. Can someone tell me if this is 
available for other Haskell systems? And didn't GHC also provide binary I/O?

> However, a simple and fairly generic mechanism for doing this is:
>
> 1. Read in a list of "Char"s with the standard I/O functions.

This will most likely cause problems under Windows. The reason is that the 
standard I/O functions are intended for reading and writing text, and that's 
why under Windows the sequence CRLF (#0D#0A) is interpreted as a single LF 
(#0A) and EOF (#1A) is interpreted as the end of the file.

> 2. Use "map (fromIntegral . ord) ..." to get a list of "Word8"s (octets).

This assumes that the standard I/O functions use a one byte character 
encoding. I don't now whether this is guaranteed by the Haskell report.

> [...]

Wolfgang