[Haskell-cafe] Endian conversion

Benjamin Franksen benjamin.franksen at bessy.de
Fri Oct 7 12:41:43 EDT 2005


On Friday 07 October 2005 16:51, Joel Reymont wrote:
> It seems that (BigEndian x) <- peek ptr_x is the culprit.
>
> On Oct 7, 2005, at 4:48 PM, Joel Reymont wrote:
> > This code hangs for me for whatever reason when I run it at the
> > ghci prompt. It does not matter whether I load it from a file or
> > type it in.

This is because I got the peek vs. peekByteOff semantics completely 
wrong. Sorry for that. (peekByteOff i) does exactly the same as peek, 
only with a pointer that is advanced (i) bytes forward. That means, the 
code I sent reads and writes to random memory locations; no wonder the 
program hangs (I am astonished it doesn't crash).

In order to define byte swapping instances of Storable, you have to swap 
the bytes /in memory/ and afterwards interprete them as whatever value 
they were originally. This is like C programming in Haskell, which is 
possible, but no less ugly than the C version.

Cheers,
Ben


More information about the Haskell-Cafe mailing list