; $Id: swap_endian.pro,v 1.13 2001/01/15 22:28:13 scottm Exp $ ; ; Copyright (c) 1993-2001, Research Systems, Inc. All rights reserved. ; Unauthorized reproduction prohibited. ;+ ; NAME: ; SWAP_ENDIAN ; ; PURPOSE: ; This fucntion reverses the byte ordering of arbitrary scalars, ; arrays or structures. It may be used, for example, to make little ; endian numbers big, or big endian numbers little. ; ; CATEGORY: ; Utility. ; ; CALLING SEQUENCE: ; Result = SWAP_ENDIAN(A) ; ; INPUTS: ; A: The scalar, array, or structure to be swapped. ; ; OUTPUTS: ; Result: The same type and structure as the input, with the ; pertinent bytes reversed. ; ; PROCEDURE: ; Swap arrays and scalars directly using BYTEORDER. ; Swap structures recursively. ; ; EXAMPLE: ; A = SWAP_ENDIAN(A) ;Reverses the "endianness" of A ; ; MODIFICATION HISTORY: ; DMS, RSI, May, 1993. Written. ; DMS, RSI, July, 1993. Added double complex. ; AB, RSI, 5 October 1998, Fixed double complex case and updated for ; pointer, object reference, unsigned 16, 32, and 64-bit ; integers, and 64-bit signed integers. ;- function swap_endian, in t = in ;Make a copy case (size(t, /TYPE)) of 1: return, t ; BYTES require no swapping. 2: byteorder, t, /SSWAP ; 16-bit signed integers 3: byteorder, t, /LSWAP ; 32-bit signed long integers 4: byteorder, t, /LSWAP ; Single floats 5: byteorder, t, /L64SWAP ; Double floats 6: byteorder, t, /LSWAP ; Single complex 7: return, t ; Strings require no swapping 8: for i=0, n_tags(t)-1 do begin ; Handle structures recursively. temp = swap_endian(t.(i)) t.(i) = temp endfor 9: byteorder, t, /L64SWAP ; Double complex. 10: message, 'unable to swap pointer data type' 11: message, 'unable to swap object reference data type' 12: byteorder, t, /SSWAP ; Unsigned shorts 13: byteorder, t, /LSWAP ; Unsigned Longs 14: byteorder, t, /L64SWAP ; Signed 64-bit integers 15: byteorder, t, /L64SWAP ; Unsigned 64-bit integers else: message, 'Internal error: unknown type' ENDCASE return, t end