[Gllug] endian.h

Tethys tet at accucard.com
Wed Feb 5 09:15:07 UTC 2003


Mike Brodbelt writes:

>One of the source files makes use of functions like betoh32(),
>letoh32(), and also the 16bit variants of those. On OpenBSD, the
>function prototypes are in <machine/endian.h>, but, though Linux
>has an endian.h file, those functions aren't in it.

Assuming they behave as their names imply:

	#include <endian.h>
	#include <netinet/in.h>

	#if __BYTE_ORDER == __LITTLE_ENDIAN
	# define betoh32 ntohl
	# define letoh32(x) (x)
	#else
	# define betoh32(x) (x)
	# define letoh32(x) \
		((((x) & 0xff) << 24) + \
		(((x) & 0xff00) << 8) + \
		(((x) & 0xff0000) >> 8) + \
		(((x) & 0xff000000) >> 24))
	#endif

There are probably more efficient ways of doing it than the bit twiddling
I've done, but it should at least work...

Tet

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list