[Haskell-cafe] Endian conversion
Benjamin Franksen
benjamin.franksen at bessy.de
Fri Oct 7 10:39:29 EDT 2005
On Friday 07 October 2005 14:50, Benjamin Franksen wrote:
> First a version that should work (at least it compiles) for poke:
>
> \begin{code}
> instance (Storable a) => Storable (Endian a) where
> sizeOf (Endian a _) = sizeOf a
> alignment (Endian a _) = alignment a
> pokeByteOff p i (Endian a b) =
> if getHostByteOrder == b
> then pokeByteOff p i a
> else pokeByteOff p (sizeOf a - 1 - i) a
> \end{code}
Note that even this breaks down for more complex types (i.e. records).
For instance, with the obvious Storable instance of (Int16,Int16), the
elements will get swapped, too. I can't see how to solve this in a
generic way.
Ben
More information about the Haskell-Cafe
mailing list