Portability vs. Endianness
Peter da Silva
peter at ficc.ferranti.com
Fri Mar 15 00:59:29 AEST 1991
In article <1991Mar12.105451.19488 at dit.upm.es> esink at turia.dit.upm.es () writes:
> long var;
> unsigned char Bytes[4];
>
> Is there a portable way to move the value held in var
> into the memory space pointed to by Bytes, with the restriction
> that the representation be in Most Significant Byte first
> format ? I am well aware that sizeof(long) may not be 4. I
> want the value contained in var converted to a 68000
> long word, and I want the code fragment to run on any
> machine. The solution must be ANSI C.
Bytes[0] = (var & 0xFF000000) >> 24;
Bytes[1] = (var & 0x00FF0000) >> 16;
Bytes[2] = (var & 0x0000FF00) >> 8;
Bytes[3] = (var & 0x000000FF) >> 0;
If you don't mind checking ifdefs, you can run a test program to check
endianness and do this:
#ifdef E4321
*((long *)Bytes) = var
#else
... above code ...
#endif
One guy here wrote a fairly extensive .h file for manipulating things
in 68000 order. It needs a single #define for the native byte order and
defines everything else based on that.
--
Peter da Silva. `-_-' peter at ferranti.com
+1 713 274 5180. 'U` "Have you hugged your wolf today?"
More information about the Comp.lang.c
mailing list