[Dev] byte swapper

Ed Hill dev@trilug.org
28 May 2002 21:29:31 -0600


On Tue, 2002-05-28 at 21:00, Michael Mueller wrote:
> Is there a byte swapper for big-to-little, and vice-versa, endian interchange 
> lurking about, or shall I be forced to write yet another?

Hi Mike,

I've used these.

hth,
Ed



#ifdef LITTLE_ENDIAN

/* Little-endian architectures need byte-swapping. */
#define sws(x) (((x >> 8) & 0x00ff) | ((x << 8) & 0xff00))
#define swl(x) (sws(x >> 16) | (sws(x) << 16))
#define swap_short(x) (x = sws(x))
#define swap_long(x) (x = swl(x))

#else

/* Big-endian architectures don't need byte-swapping. */
#define sws(x) (x)
#define swl(x) (x)
#define swap_short(x) (x = sws(x))
#define swap_long(x) (x = swl(x))

#endif


-- 
Edward H. Hill III, PhD    |  Email:       ed@eh3.com, ehill@mines.edu
Post-Doctoral Researcher   |  URLs:        http://www.eh3.com
Division of ESE            |   http://wasser.mines.edu/people/edhill.php
Colorado School of Mines   |  Phone:       303-273-3483
Golden, CO  80401          |  Fax:         303-273-3311
Key fingerprint = 5BDE 4DA1 66BE 4F7B BC17  3A0C 932B 7266 1E76 F123