
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 it. This standard network library stuff
is all sorted out for C programmers, it's just a matter of getting to it.
Code for htonl follows, htons should obvious. For floats, I suppose
you'd deliver them across the interface as whatever the floating point
equivalent of CInt, then make sure they're 32 bit floats on the C
side before swapping them with htonl.
Donn Cave, donn@drizzle.com
--- Netword.hsc ---
-# OPTIONS -fffi #-}
module Netword (htonl,ntohl) where
import Foreign
import Foreign.C
#include "netw.h"
foreign import ccall unsafe "c_htonl" htonl :: CInt -> CInt
foreign import ccall unsafe "c_ntohl" ntohl :: CInt -> CInt
--- netw.c ---
#include