Byte Order

Little-endian and big-endian integer conversion. More...

Functions

int8_t swab (int8_t value)
 Returns value unchanged.
uint8_t swab (uint8_t value)
 Returns value unchanged.
bool isBigEndian ()
 Returns true if the host stores the most significant byte first.
bool isLittleEndian ()
 Returns true if the host stores the least significant byte first.
template<typename T>
hostToLe (const T &value)
 Converts value from host order to little-endian.
template<typename T>
leToHost (const T &value)
 Converts value from little-endian to host order.
template<typename T>
hostToBe (const T &value)
 Converts value from host order to big-endian.
template<typename T>
beToHost (const T &value)
 Converts value from big-endian to host order.

Detailed Description

A multi-byte integer has a byte order. On a little-endian host the least significant byte is stored first; on a big-endian host the most significant byte is stored first. File formats and network protocols pick one of those orders. This group converts between that external order and the order of the CPU that runs the program.

The host order is a compile-time fact. The headers define PT_LE or PT_BE from the toolchain, or from an explicit build setting. isLittleEndian() and isBigEndian() answer the same question at run time by inspecting a known integer in memory. Prefer the compile-time macros when the conversion can vanish on a matching host. Use the run-time queries when the answer must be a value.

swab() reverses the bytes of a fixed-size integer from Fixed-Size Integers. An 8-bit value is unchanged. 16-bit, 32-bit, and 64-bit values swap every byte. swab() always swaps; it does not know about host order. Overload swab() for a type that is not an integer when that type has a defined byte-wise reverse.

hostToLe() and leToHost() convert between host order and little-endian. On a little-endian host they return the value unchanged. On a big-endian host they call swab(). hostToBe() and beToHost() do the same for big-endian: they are no-ops on a big-endian host and swap on a little-endian host. Passing a value that is already in host order through hostToLe() produces the little-endian layout to store in a file or on the wire. Passing a little-endian value from a file through leToHost() produces a host integer that arithmetic can use.

Pt::uint32_t host = 0x01020304;
T hostToLe(const T &value)
Converts value from host order to little-endian.
Definition Byteorder.h:220
T leToHost(const T &value)
Converts value from little-endian to host order.
Definition Byteorder.h:238
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52

The conversions are templates on the integer type. They expect a type that swab() already handles, or a type with its own swab() overload. Mixing a host integer with an external integer without these functions is the usual mistake: the value looks right on one endianness and silently wrong on the other.

Function Documentation

◆ swab() [1/2]

int32_t swab ( int8_t value)

Swaps the bytes of a 32-bit integer.

Swaps the bytes of a 16-bit integer.

◆ swab() [2/2]

uint32_t swab ( uint8_t value)

Swaps the bytes of a 32-bit integer.

Swaps the bytes of a 16-bit integer.

◆ hostToLe()

template<typename T>
T hostToLe ( const T & value)

Returns value unchanged on a little-endian host. Calls swab() on a big-endian host. Overload swab() for types that are not fixed-size integers.

◆ leToHost()

template<typename T>
T leToHost ( const T & value)

Returns value unchanged on a little-endian host. Calls swab() on a big-endian host. Overload swab() for types that are not fixed-size integers.

◆ hostToBe()

template<typename T>
T hostToBe ( const T & value)

Returns value unchanged on a big-endian host. Calls swab() on a little-endian host. Overload swab() for types that are not fixed-size integers.

◆ beToHost()

template<typename T>
T beToHost ( const T & value)

Returns value unchanged on a big-endian host. Calls swab() on a little-endian host. Overload swab() for types that are not fixed-size integers.