[Haskell-cafe] Endian conversion
coeus at gmx.de
Mon Oct 3 11:31:15 EDT 2005
nice project. (except that winonly-closedsource-....thing. my condolence.)
on which platform are you programming? mac? linux-ppc?
i see that you understood most of that code.
big-endian-test: the number 1234 has two ends (like a sausage), the end with the 1 is the big end (1000), the 4 is the little one. if you save he number 1 as int in little endian, then you write the bytes 01 00 00 00, and in big endian it is 00 00 00 01. so, if you read the first char, then it will either be ==1 (little) or /=1 (big).
to write that LittleEndian stuff, you only need to replace Big with Little and "if be then" with "if not be then". it is the question wether to use he function xyz0(derived) or xyzR(reverse order of bytes). i hope, that the compiler optimizes "if True" away.
to use the Storables, read the docu about the libs(functions)
you just need to read "LittleEndian CInt", remove that "LittleEndian"-constructor, work with that CInt, put the "BigEndian"-constructor at that CInt, write it...
maybe you need to convert LE and BE enums, too. use "(toEnum . fromEnum)" to convert between any enum and CInt.
Joel Reymont wrote:
> Well, I liked that bit of Template Haskell code that Marc sent. I'm
> now stuck trying to adapt it to read Storables :-).
> It seems, on a second glance, that there's not that much to adapt. If
> I read Marc's code correctly it "derives" Storable and uses the peek,
> etc. methods to swap bytes around. Which means to me that so long as
> the byte swapping methods are implemented and I try to store a
> BigEndian or LittleEndian it would be stored correctly for me.
> Is this so?
> To recap, I'm trying to read binary packets from a socket and the
> first thing I do is read the packet length. I then need to read the
> packet body where the numbers are little or big endian. After
> processing the packet I need to write it out and the numbers again
> could be little or big endian.
> I could read a FastString from a socket since it has IO methods but I
> don't know how to convert the FS into a pointer suitable for
> Storable. So much to learn :-).
> Thanks, Joel
> On Oct 3, 2005, at 9:33 PM, Udo Stenzel wrote:
> > Why don't you pull out 4 bytes and assemble them manually? Three
> > shifts, logical ors and fromIntegrals aren't that much of a burden
> > after
> > all.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe