Regarding NewBinary... I think my challenge is how to add endian- conversion without duplicating all the put and get methods from NewBinary.

I would still use that bit of TH code to figure out whether my platform is big or little endian. I don't care about cross- compilation and what that code does is analogous to #ifdef ... #define LITTLE_ENDIAN ... #endif, etc.

I'm looking for architectural suggestions, though. Should I define LittleEndian Word32, BigEndian Word16, etc. or should I have Endian Word32 Big, Endian Word16 Little, etc.

    Thanks, Joel

On Oct 5, 2005, at 11:42 AM, Udo Stenzel wrote:

Why don't you pull out 4 bytes and assemble them manually?


To that I'd like to add a snippet from NewBinary itself:

| instance Binary Word32 where
|   put_ h w = do
|     putByte h (fromIntegral (w `shiftR` 24))
|     putByte h (fromIntegral ((w `shiftR` 16) .&. 0xff))
|     putByte h (fromIntegral ((w `shiftR` 8)  .&. 0xff))
|     putByte h (fromIntegral (w .&. 0xff))
|   get h = do
|     w1 <- getWord8 h
|     w2 <- getWord8 h
|     w3 <- getWord8 h
|     w4 <- getWord8 h
|     return $! ((fromIntegral w1 `shiftL` 24) .|.
|            (fromIntegral w2 `shiftL` 16) .|.
|            (fromIntegral w3 `shiftL`  8) .|.
|            (fromIntegral w4))

This obviously writes a Word32 in big endian format, also known as
"network byte order", and doesn't care how the host platform stores
integers. No need for `hton' and `ntoh'. To convert it to write little
endian, just copy it and reorder some lines.  (But I think, writing LE
integers with no good reason and without an enclosing protocol that
explicitly declares them (like IIOP) is a bad idea.)

--
http://wagerlabs.com/idealab





_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to