[Haskell-cafe] Endian conversion
Joel Reymont
joelr1 at gmail.com
Thu Oct 6 12:48:49 EDT 2005
I don't want to replicate all the code in NewBinary for Little/Big
endian. I'm looking for an elegant solution (Haskell, the elegant
language, you know).
I don't care about cross-compiling stuff and the server that I need
to work with runs on Wintel whereas I can be either on Windows or Mac
or Linux.
Last but not least, I also need to read floats. These would be IEEE
754 (standard) but I still need to swap the bytes around.
I still don't know how to do this without rewriting all the get/put
functions in NewBinary. Marc Ziegert's code is very hepful but still
incomplete. I think Marc's TH bit
be = $( (1::CChar)/=(unsafePerformIO $ with (1::CInt) $ peekByteOff
`flip` 0) ) :: Bool
is the way to determine if you are BE or LE but how do I use it to
read a Float and a 32-bit Int for example?
If anyone would be kind enough to give me a complete example I would
appreciate it.
Thanks, Joel
On Oct 5, 2005, at 11:42 AM, Udo Stenzel wrote:
> 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
More information about the Haskell-Cafe
mailing list