BYTEORDER Procedure

Converts integers between host and network byte ordering. This procedure can also be used to swap the order of bytes within both short and long integers.

Note: For simple byte-swapping without network considerations and for swapping floating point data, see the BYTESWAP Procedure.
Note: A 'Network Long' is limited to 4 bytes/32-bits. You should use the Ntohl and Htonl keywords only for 32-bit integer data types, i.e., the PV‑WAVE INT32 and LONG types in 32-bit PV‑WAVE and the PV‑WAVE INT32 type in 64-bit PV‑WAVE. Using these keywords on a 64-bit integer causes it to be truncated at 4-bytes with possible loss/corruption of data.

Usage

  • BYTEORDER, variable1, ..., variablen

  • BYTEORDER, variable1, ..., variablen

Input Parameters

variablei—A variable (see Discussion below).

Output Parameters

variablei—A variable (see Discussion below).

Keywords

Htonl—Host to network, 32-bit integers. Using the Htonl keyword on a 64-bit integer causes it to be truncated at 4 bytes with possible loss/corruption of data.

Htons—Host to network, 16-bit short integers, i.e., the PV‑WAVE INT data type.

Lswap—Swaps the order of the bytes in PV‑WAVE LONG variables.

In 32-bit PV‑WAVE, the four bytes in a 32-bit PV‑WAVE LONG are changed from (B0,B1,B2,B3) to (B3, B2,B1, B0).

In 64-bit PV‑WAVE, the eight bytes in a 64-bit PV‑WAVE LONG are changed from (B0,B1,B2,B3,B4,B5,B6, B7) to (B7,B6,B5,B4,B3, B2,B1, B0).

Ntohl—Network to host, 32-bit integers. Using the Ntonl keyword on a 64-bit integer causes it to be truncated at 4 bytes with possible loss/corruption of data.

Ntohs—Network to host, 16-bit short integers, i.e. the PV‑WAVE INT data type.

Sswap—16-bit Short integer swap. Always swaps the bytes within 16-bit short integers, i.e., the PV‑WAVE INT data type. The even and odd numbered bytes are interchanged.

Discussion

BYTEORDER is most commonly used when dealing with binary data from non-native architectures that may have different byte ordering. An easier solution to use when reading or writing this sort of data is the XDR format, as explained in the PV‑WAVE Programmer’s Guide.

The size of the parameter, in bytes, must be evenly divisible by two for 16-bit short integer swaps, and by four for 32-bit integer swaps. BYTEORDER operates on both scalars and arrays. The parameter must be a variable, not an expression or constant, and may not contain strings.

Note: The contents of variablei  are overwritten by the result.

The Ntonl and Htonl keywords do not work on 64-bit PV‑WAVE LONG integers because the 'network long' format supports only 32-bit values. In 64-bit PV‑WAVE, you should only use the Ntohl and Htonl keywords for INT32 data types and use the Lswap keyword when operating on PV‑WAVE LONG data types. Note that the Lswap keyword always performs an unconditional swap.

Network byte ordering is big endian. On big-endian machines, the Ntonl and Htonl keywords do nothing. However, the Lswap keyword does not take the current platform into account and always performs the byteswapping. Users will need to know about the origin of their data in order to make the decision as to whether or not byteswapping needs to be performed, i.e. If the 64-bit data was written out with different endianess than the machine on which it is being read, byteswapping should be performed.

Examples

; Form a hexadecimal value that can be easily interpreted. Note
; that the hex value "12" is in the high-order byte, and "34" is
; in the low order byte.
a = '1234'X
; Remember that b will be overwritten by BYTEORDER.
b = a
BYTEORDER, b, /Sswap
PRINT, Format='(2Z9)', a, b
; PV-WAVE prints: 1234     3412
; The result shows that the high- and low-order bytes in b have
; been switched.
a = '12345678'XL
b = a
BYTEORDER, b, /Lswap
PRINT, Format='(2Z9)', a, b ; PV-WAVE prints: 12345678 78563412

Bytes in b are swapped as expected, whereas in hexadecimal format, two digits represent a single byte.

See Also

BYTE, BYTESWAP