Swapping data if required

Suppose you need to ensure that data obtained in the host order (i.e., whatever is “native” on this machine) is returned in a particular order, either big- or little-endian. Or vice versa: you want to convert data from host order to big- or little-endian.

You can use the following macros (described here as if they were functions for syntactic convenience):

uint16_t ENDIAN_LE16 (uint16_t var )
If the host is little-endian, this macro does nothing (expands simply to var); else, it performs a byte swap.
uint32_t ENDIAN_LE32( uint32_t var )
If the host is little-endian, this macro does nothing (expands simply to var); else, it performs a quadruple byte swap.
uint64_t ENDIAN_LE64( uint64_t var )
If the host is little-endian, this macro does nothing (expands simply to var); else, it swaps octets of bytes.
uint16_t ENDIAN_BE16( uint16_t var )
If the host is big-endian, this macro does nothing (expands simply to var); else, it performs a byte swap.
uint32_t ENDIAN_BE32( uint32_t var )
If the host is big-endian, this macro does nothing (expands simply to var); else, it performs a quadruple byte swap.
uint64_t ENDIAN_BE64( uint64_t var )
If the host is big-endian, this macro does nothing (expands simply to var); else, it swaps octets of bytes.