On Wed, 26 Feb 2003, Hans-Christoph Steiner wrote:
In effort to learn how to port stuff to MacOS X, I was hoping someone could point me to a piece of code where endianness is an issue and has been ported to PowerPC. I always learn best by following an good example.
suppose you have:
char data[4]; /* float data directly from a file */
then this is wrong:
*(float *)data
you have to do this:
1. "convert" to native integer
1.1. from little-endian: int x = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
1.2. from big-endian: int x = data[3] | (data[2]<<8) | (data[1]<<16) | (data[0]<<24);
2. "convert" to float *(float *)&x
this works in similar fashion for reading other number types, except for "short" the formulas are shorter, and for "double" and "long long" the formulas are longer. step 1 needs to be done on an integer type of the same size as the data. step 2 is useless if desired result is an integer type...
GridFlow has been tested on a few bigendians: Sun, SGI, Mac. However I fear the code I'd extract from it wouldn't be too obvious. I hope the above explanation is sufficient.
________________________________________________________________ Mathieu Bouchard http://artengine.ca/matju