[Haskell-cafe] Endian conversion

Joel Reymont joelr1 at gmail.com
Fri Oct 7 10:42:41 EDT 2005

What I have in mind is composing a packet structure from a list of  
storables. These would be the packet fields. Alternatively, I guess I  
could declare my record to be an instance of storable and implement  
the peek, poker, etc. Would this work?

On Oct 7, 2005, at 4:39 PM, Benjamin Franksen wrote:

> 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.


More information about the Haskell-Cafe mailing list