Re: [Haskell-cafe] Endian conversion

2005-10-07 Thread Benjamin Franksen
On Friday 07 October 2005 12:50, Joel Reymont wrote: I tried to generalize the endian-related code and came up with something like the following which does not compile. What am I doing wrong? I would like Endian to be a wrapper around Storable with the endian flag. I want to be able to

Re: [Haskell-cafe] Endian conversion

2005-10-07 Thread Benjamin Franksen
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

Re: [Haskell-cafe] Endian conversion

2005-10-07 Thread Joel Reymont
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

Re: [Haskell-cafe] Endian conversion

2005-10-07 Thread Joel Reymont
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. On Oct 7, 2005, at 2:50 PM, Benjamin Franksen wrote: You would probably want to wrap/unwrap values (using BigEndian/LittleEndian) just before/after

Re: [Haskell-cafe] Endian conversion

2005-10-07 Thread Joel Reymont
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. On Oct 7, 2005, at 2:50 PM, Benjamin

Re: [Haskell-cafe] Endian conversion

2005-10-07 Thread Benjamin Franksen
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

Re: [Haskell-cafe] Endian conversion

2005-10-06 Thread Joel Reymont
Regarding NewBinary... I think my challenge is how to add endian- conversion without duplicating all the put and get methods from NewBinary. I would still use that bit of TH code to figure out whether my platform is big or little endian. I don't care about cross- compilation and what that

Re: [Haskell-cafe] Endian conversion

2005-10-06 Thread Joel Reymont
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

Re: [Haskell-cafe] Endian conversion

2005-10-06 Thread Donn Cave
On Thu, 6 Oct 2005, Joel Reymont wrote: 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). Maybe that's why I haven't seen anyone propose a foreign interface, but it's sure how I would do

Re: [Haskell-cafe] Endian conversion

2005-10-06 Thread Joel Reymont
Why doesn't this compile? be = $( (1::CChar) /= (unsafePerformIO $ with (1::CInt) $ peekByteOff `flip` 0) ) :: Bool Endian.hs:10:8: Couldn't match `Language.Haskell.TH.Lib.ExpQ' against `Bool' Expected type: Language.Haskell.TH.Lib.ExpQ

Re: [Haskell-cafe] Endian conversion

2005-10-06 Thread Joel Reymont
Solution by TheHunter on #haskell: be = $(lift $ (1::CChar) /= (unsafePerformIO $ with (1::CInt) $ peekByteOff `flip` 0) ) :: Bool Thanks, Joel On Oct 6, 2005, at 9:13 PM, Joel Reymont wrote: Why doesn't this compile? be = $(

Re: [Haskell-cafe] Endian conversion

2005-10-05 Thread Udo Stenzel
Why don't you pull out 4 bytes and assemble them manually? To that I'd like to add a snippet from NewBinary itself: | instance Binary Word32 where | put_ h w = do | putByte h (fromIntegral (w `shiftR` 24)) | putByte h (fromIntegral ((w `shiftR` 16) .. 0xff)) | putByte h

Re: [Haskell-cafe] Endian conversion

2005-10-05 Thread Marc Ziegert
you are right, that pice of code is ugly. i would write sth simmilar (Int32-[Word8]) like you did, iff it should be able to cross-compile or do not need to be fast or should not need TH. well, i think, in the case of joel's project the last sentence means ..., iff true or true or undefined. is

Re: [Haskell-cafe] Endian conversion

2005-10-04 Thread John Meacham
On Mon, Oct 03, 2005 at 09:33:11PM +0200, Udo Stenzel wrote: Joel Reymont wrote: Are there any endian conversion routines for Haskell? I'm looking to build binary packets on top of NewBinary.Binary but my data is coming in little-endian whereas I'll need to send it out big endian.

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Tomasz Zielonka
On 10/3/05, Joel Reymont [EMAIL PROTECTED] wrote: Folks,Are there any endian conversion routines for Haskell? I'm looking tobuild binary packets on top of NewBinary.Binary but my data is comingin little-endian whereas I'll need to send it out big endian. >From your question I assume you want

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Marc Ziegert
well, fastest conversion to compute could be an assembler-command, but if we don't use that, it could be converted via Foreign.Storable and sth like the following: (i did not test it, and i hope, TH works like this...) data (Integral a) = BigEndian a = BigEndian a deriving (Eq,Ord,Enum,...) be

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Joel Reymont
Well, I'm looking for suggestions on how to implement this. I'll basically get a chunk of data from the socket that will have things little-endian and will need to send out a chunk that will have the numbers big-endian. This is a proxy server that does binary protocol conversion. It's a

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Marc Ziegert
for just making IO and a little bit-conversion, i would use c++ or even c. for such a problem you have to be near the machine, not necessarily near mathematical abstraction. there exist assembler-commands to flip endians of register-values, so i would just search in /usr/include/*/* for a

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Joel Reymont
On Oct 3, 2005, at 6:51 AM, Marc Ziegert wrote: data (Integral a) = BigEndian a = BigEndian a deriving (Eq,Ord,Enum,...) be = $( (1::CChar)/=(unsafePerformIO $ with (1::CInt) $ peekByteOff `flip` 0) ) :: Bool Will this always correctly determine if the platform is big-endian? How does

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Udo Stenzel
Joel Reymont wrote: Are there any endian conversion routines for Haskell? I'm looking to build binary packets on top of NewBinary.Binary but my data is coming in little-endian whereas I'll need to send it out big endian. Why don't you pull out 4 bytes and assemble them manually? Three

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Joel Reymont
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

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Mark Carroll
On Mon, 3 Oct 2005, Joel Reymont wrote: On Oct 3, 2005, at 6:51 AM, Marc Ziegert wrote: data (Integral a) = BigEndian a = BigEndian a deriving (Eq,Ord,Enum,...) be = $( (1::CChar)/=(unsafePerformIO $ with (1::CInt) $ peekByteOff `flip` 0) ) :: Bool Will this always correctly

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Marc Ziegert
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

Re: [Haskell-cafe] Endian conversion

2005-10-03 Thread Tomasz Zielonka
On 10/3/05, Udo Stenzel [EMAIL PROTECTED] wrote: Joel Reymont wrote: Are there any endian conversion routines for Haskell? I'm looking to build binary packets on top of NewBinary.Binary but my data is coming in little-endian whereas I'll need to send it out big endian. Why don't you pull out 4

[Haskell-cafe] Endian conversion

2005-10-02 Thread Joel Reymont
Folks, Are there any endian conversion routines for Haskell? I'm looking to build binary packets on top of NewBinary.Binary but my data is coming in little-endian whereas I'll need to send it out big endian. Thanks, Joel -- http://wagerlabs.com/idealab