[Haskell-cafe] binary IO

Pupeno pupeno at pupeno.com
Tue Dec 27 10:12:37 EST 2005


On Tuesday 27 December 2005 02:10, Jeremy Shaw wrote:
> they do not care about big endian vs little endian.
Does it mean that it just reads the data in whatever endianess the computer is 
in, right ?

> However, while the Binary class in NewBinary may not be appropriate,
> the uniform interface to binary files/memory could be a good foundation
> for building hGetWord16le, hGetWord16be, etc.
>
> If you submitted a module that added these functions, I would be glad
> to update the archive.
I've been reading NewBinary's code, it is a bit intimidating for me[1]. It 
seems easier to define my own binary functions[2]. Could you point me a bit 
what is the unifor interface so I might give it another chance (to work with 
NewBinary) ?
Thanks.
-- 
Pupeno <pupeno at pupeno.com> (http://pupeno.com)

PS: Is anything wrong with [2] ?

[1] I've been with Haskell for a little more than a couple of weeks.

[2] Doing some experimentation I wrote:

-- | Read a Word8 from a Ptr of any type.
ptrToWord8 p = do
  let np :: Ptr Word8
      np = castPtr p
  r <- (peek np)
  return r

-- | Read a Word16 Big Endian from a Ptr of any type.
ptrToWord16BE p = do
  b1 <- ptrToWord8 p
  b2 <- ptrToWord8 (plusPtr p 1)
  let nb1 :: Word16
      nb1 = fromIntegral b1
      nb2 :: Word16
      nb2 = fromIntegral b2
  return ((shift nb1 8) .|. nb2)

-- | Read a Word16 Little Endian from a Ptr of any type.
ptrToWord16LE p = do
  b1 <- ptrToWord8 p
  b2 <- ptrToWord8 (plusPtr p 1)
  let nb1 :: Word16
      nb1 = fromIntegral b1
      nb2 :: Word16
      nb2 = fromIntegral b2
  return ((shift nb2 8) .|. nb1)

 which I used to read Word16s successfully.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20051227/114ef00a/attachment.bin


More information about the Haskell-Cafe mailing list