4232
Byte ordering schemes used by IDL platforms
When reading data, created by a Sun Sparc machine, on a Windows XP machine it turns out to be nonsense? Why is this so?
This problem is the result of how machines store data. Sun Sparc machines are "Big Endian" machines, meaning that multiple byte integers are stored in memory beginning with the most significant byte. On the other hand, Windows XP machines are "Little Endian", meaning that multiple byte integers are stored in memory beginning with the least significant byte. To read data written by the Sun Sparc with the Windows XP machine or vice versa use the SWAP_ENDIAN function with either the SWAP_IF_LITTLE_ENDIAN or SWAP_IF_BIG_ENDIAN keyword.
Note
The keyword
does not refer to the byte ordering of the input data, but to the byte ordering of the computer hardware.
If using the SWAP_ENDIAN function seems cumbersome then consider writing out the files using the XDR keyword. For example:
;Open a file for XDR output.
OPENW, /XDR, 1, 'data.dat'
XDR is a machine independent format, that allows data written on one machine to be read transparently on any other machine.
PROCESSOR TYPE |
OPERATING SYSTEM |
BYTE ORDERING |
|
Intel X86 |
Linux |
little-endian |
|
Solaris X86 |
little-endian |
|
Windows |
little-endian |
|
Macintosh |
little-endian |
Sun Sparc |
SunOS |
big-endian |
|
Solaris |
big-endian |
Another easy way to determine the byte ordering for your system from an IDL prompt is to issue the command:
print,'This machine is: ', byte(1,0)?'little endian':'big endian'