BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_byteorder

Macros

#define BSLS_BYTEORDER_NTOH(x)   BloombergLP::bsls::ByteOrderUtil::swapBytes(x)
 
#define BSLS_BYTEORDER_NTOHS(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes16(x)
 
#define BSLS_BYTEORDER_NTOHL(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes32(x)
 
#define BSLS_BYTEORDER_NTOHLL(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes64(x)
 
#define BSLS_BYTEORDER_HTON(x)   BSLS_BYTEORDER_NTOH(x)
 
#define BSLS_BYTEORDER_HTONS(x)   BSLS_BYTEORDER_NTOHS(x)
 
#define BSLS_BYTEORDER_HTONL(x)   BSLS_BYTEORDER_NTOHL(x)
 
#define BSLS_BYTEORDER_HTONLL(x)   BSLS_BYTEORDER_NTOHLL(x)
 
#define BSLS_BYTEORDER_NTOHS_CONSTANT(x)
 
#define BSLS_BYTEORDER_NTOHL_CONSTANT_NO_MSB(x)
 
#define BSLS_BYTEORDER_NTOHL_CONSTANT(x)
 
#define BSLS_BYTEORDER_NTOHLL_CONSTANT_NO_MSB(x)
 
#define BSLS_BYTEORDER_NTOHLL_CONSTANT(x)
 
#define BSLS_BYTEORDER_HTONS_CONSTANT(x)   BSLS_BYTEORDER_NTOHS_CONSTANT(x)
 
#define BSLS_BYTEORDER_HTONL_CONSTANT(x)   BSLS_BYTEORDER_NTOHL_CONSTANT(x)
 
#define BSLS_BYTEORDER_HTONLL_CONSTANT(x)   BSLS_BYTEORDER_NTOHLL_CONSTANT(x)
 
#define BSLS_BYTEORDER_LE_U16_TO_HOST(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes16(x)
 
#define BSLS_BYTEORDER_LE_U32_TO_HOST(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes32(x)
 
#define BSLS_BYTEORDER_LE_U64_TO_HOST(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes64(x)
 
#define BSLS_BYTEORDER_HOST_U16_TO_LE(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes16(x)
 
#define BSLS_BYTEORDER_HOST_U32_TO_LE(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes32(x)
 
#define BSLS_BYTEORDER_HOST_U64_TO_LE(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes64(x)
 
#define BSLS_BYTEORDER_BE_U16_TO_HOST(x)   (x)
 
#define BSLS_BYTEORDER_BE_U32_TO_HOST(x)   (x)
 
#define BSLS_BYTEORDER_BE_U64_TO_HOST(x)   (x)
 
#define BSLS_BYTEORDER_HOST_U16_TO_BE(x)   (x)
 
#define BSLS_BYTEORDER_HOST_U32_TO_BE(x)   (x)
 
#define BSLS_BYTEORDER_HOST_U64_TO_BE(x)   (x)
 
#define BSLS_BYTEORDER_LE_TO_HOST(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes(x)
 
#define BSLS_BYTEORDER_HOST_TO_LE(x)    BloombergLP::bsls::ByteOrderUtil::swapBytes(x)
 
#define BSLS_BYTEORDER_BE_TO_HOST(x)   (x)
 
#define BSLS_BYTEORDER_HOST_TO_BE(x)   (x)
 

Detailed Description

Outline

Purpose

Provide byte-order manipulation macros.

Classes

See also
bsls_byteorderutil

Macros

Description

This component provides a set of byte-order manipulation macros that replace the standard htonl, htons, ntohl, and ntohs functions, and which do not require including any system header files:

#define BSLS_BYTEORDER_HTONS(x)
Definition bsls_byteorder.h:272
#define BSLS_BYTEORDER_HTONL(x)
Definition bsls_byteorder.h:274
#define BSLS_BYTEORDER_NTOHLL(x)
Definition bsls_byteorder.h:267
#define BSLS_BYTEORDER_NTOH(x)
Definition bsls_byteorder.h:259
#define BSLS_BYTEORDER_NTOHL(x)
Definition bsls_byteorder.h:264
#define BSLS_BYTEORDER_HTON(x)
Definition bsls_byteorder.h:270
#define BSLS_BYTEORDER_HTONLL(x)
Definition bsls_byteorder.h:276
#define BSLS_BYTEORDER_NTOHS(x)
Definition bsls_byteorder.h:261

The "S", "L", and "LL" suffices in the names of the above macros indicate their applicability to 16-bit (short), 32-bit (int, not long), and 64-bit (long long) values, respectively.

The macros without "S", "L", or "LL" suffices in their names indicate that they take the word size to be swapped from the word size of x, and return a value of the same type as x. These should only be passed fundamental integral types, and not enum values, as the sizes of enum values are implementation defined.

This set of host-to-network and network-to-host conversion macros are very efficient, but sacrifices the ability to perform compile-time initialization. To compensate, the following set of functionally equivalent "CONSTANT" macros are provided. These macros can be used for compile-time initialization, but are less efficient than non-"CONSTANT" versions:

#define BSLS_BYTEORDER_NTOHLL_CONSTANT(x)
Definition bsls_byteorder.h:306
#define BSLS_BYTEORDER_HTONLL_CONSTANT(x)
Definition bsls_byteorder.h:314
#define BSLS_BYTEORDER_NTOHS_CONSTANT(x)
Definition bsls_byteorder.h:278
#define BSLS_BYTEORDER_HTONS_CONSTANT(x)
Definition bsls_byteorder.h:310
#define BSLS_BYTEORDER_NTOHL_CONSTANT(x)
Definition bsls_byteorder.h:293
#define BSLS_BYTEORDER_HTONL_CONSTANT(x)
Definition bsls_byteorder.h:312

Another set of macros provides conversion from big-endian or little-endian byte order to host-endian order. The macros take 16-, 32- or 64-bit values and perform the indicated byte-order conversion on those values:

#define BSLS_BYTEORDER_BE_U16_TO_HOST(x)
Definition bsls_byteorder.h:369
#define BSLS_BYTEORDER_LE_TO_HOST(x)
Definition bsls_byteorder.h:377
#define BSLS_BYTEORDER_BE_U32_TO_HOST(x)
Definition bsls_byteorder.h:370
#define BSLS_BYTEORDER_LE_U64_TO_HOST(x)
Definition bsls_byteorder.h:359
#define BSLS_BYTEORDER_LE_U16_TO_HOST(x)
Definition bsls_byteorder.h:355
#define BSLS_BYTEORDER_BE_U64_TO_HOST(x)
Definition bsls_byteorder.h:371
#define BSLS_BYTEORDER_LE_U32_TO_HOST(x)
Definition bsls_byteorder.h:357
#define BSLS_BYTEORDER_BE_TO_HOST(x)
Definition bsls_byteorder.h:381

The "LE" and "BE" embedded in the above macro names indicate Little-Endian and Big-Endian, respectively.

Finally, a complementary set of macros provides conversion from host-endian byte order to big-endian or little-endian order:

#define BSLS_BYTEORDER_HOST_U64_TO_LE(x)
Definition bsls_byteorder.h:366
#define BSLS_BYTEORDER_HOST_U32_TO_LE(x)
Definition bsls_byteorder.h:364
#define BSLS_BYTEORDER_HOST_TO_LE(x)
Definition bsls_byteorder.h:379
#define BSLS_BYTEORDER_HOST_U16_TO_BE(x)
Definition bsls_byteorder.h:373
#define BSLS_BYTEORDER_HOST_U32_TO_BE(x)
Definition bsls_byteorder.h:374
#define BSLS_BYTEORDER_HOST_TO_BE(x)
Definition bsls_byteorder.h:382
#define BSLS_BYTEORDER_HOST_U16_TO_LE(x)
Definition bsls_byteorder.h:362
#define BSLS_BYTEORDER_HOST_U64_TO_BE(x)
Definition bsls_byteorder.h:375

Usage

To use these macros, simply pass a 16-, 32-, or 64-bit value to the macros. To demonstrate the change in byte order effected by the macros, we first write a function to print, in hex, a character buffer of a specified size:

void printHex(const char *c, int size)
// Print the specified character array 'c', having the specified 'size'
// (in bytes), to 'stdout' in hex.
{
const char *hex = "0123456789abcdef";
for (int i = 0; i < size; ++i) {
std::cout << hex[(c[i] >> 4) & 0xf]
<< hex[ c[i] & 0xf];
}
}
template <class T>
void printHex(T x)
// Print the specified object 'x' of parameterized type 'T' in hex.
{
printHex((const char*)&x, sizeof x);
}

For example, to use the little-endian/big-endian to host-endian macros:

short x = static_cast<short>(0xabcd);
int y = 0xabcdef12;
bsls::Types::Int64 z = 0xabcdef1234567890LL;
// Note the use of macros within the calls to 'printHex'.
printf("\nLE to Host(x): ");
printf("\nLE to Host(y): ");
printf("\nLE to Host(z): ");
printf("\nBE to Host(x): ");
printf("\nBE to Host(y): ");
printf("\nBE to Host(z): ");
long long Int64
Definition bsls_types.h:132

On little-endian machines (e.g., x86, IA64), this will print the following to stdout:

LE to Host(x): abcd
LE to Host(y): abcdef12
LE to Host(z): abcdef1234567890
BE to Host(x): cdab
BE to Host(y): 12efcdab
BE to Host(z): 9078563412efcdab

On big-endian machines (e.g., sparc, powerpc), the following will be printed instead:

LE to Host(x): cdab
LE to Host(y): 12efcdab
LE to Host(z): 9078563412efcdab
BE to Host(x): abcd
BE to Host(y): abcdef12
BE to Host(z): abcdef1234567890

The other macros can be used in a similar manner.

Macro Definition Documentation

◆ BSLS_BYTEORDER_BE_TO_HOST

#define BSLS_BYTEORDER_BE_TO_HOST (   x)    (x)

◆ BSLS_BYTEORDER_BE_U16_TO_HOST

#define BSLS_BYTEORDER_BE_U16_TO_HOST (   x)    (x)

◆ BSLS_BYTEORDER_BE_U32_TO_HOST

#define BSLS_BYTEORDER_BE_U32_TO_HOST (   x)    (x)

◆ BSLS_BYTEORDER_BE_U64_TO_HOST

#define BSLS_BYTEORDER_BE_U64_TO_HOST (   x)    (x)

◆ BSLS_BYTEORDER_HOST_TO_BE

#define BSLS_BYTEORDER_HOST_TO_BE (   x)    (x)

◆ BSLS_BYTEORDER_HOST_TO_LE

#define BSLS_BYTEORDER_HOST_TO_LE (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes(x)

◆ BSLS_BYTEORDER_HOST_U16_TO_BE

#define BSLS_BYTEORDER_HOST_U16_TO_BE (   x)    (x)

◆ BSLS_BYTEORDER_HOST_U16_TO_LE

#define BSLS_BYTEORDER_HOST_U16_TO_LE (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes16(x)

◆ BSLS_BYTEORDER_HOST_U32_TO_BE

#define BSLS_BYTEORDER_HOST_U32_TO_BE (   x)    (x)

◆ BSLS_BYTEORDER_HOST_U32_TO_LE

#define BSLS_BYTEORDER_HOST_U32_TO_LE (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes32(x)

◆ BSLS_BYTEORDER_HOST_U64_TO_BE

#define BSLS_BYTEORDER_HOST_U64_TO_BE (   x)    (x)

◆ BSLS_BYTEORDER_HOST_U64_TO_LE

#define BSLS_BYTEORDER_HOST_U64_TO_LE (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes64(x)

◆ BSLS_BYTEORDER_HTON

#define BSLS_BYTEORDER_HTON (   x)    BSLS_BYTEORDER_NTOH(x)

◆ BSLS_BYTEORDER_HTONL

#define BSLS_BYTEORDER_HTONL (   x)    BSLS_BYTEORDER_NTOHL(x)

◆ BSLS_BYTEORDER_HTONL_CONSTANT

#define BSLS_BYTEORDER_HTONL_CONSTANT (   x)    BSLS_BYTEORDER_NTOHL_CONSTANT(x)

◆ BSLS_BYTEORDER_HTONLL

#define BSLS_BYTEORDER_HTONLL (   x)    BSLS_BYTEORDER_NTOHLL(x)

◆ BSLS_BYTEORDER_HTONLL_CONSTANT

#define BSLS_BYTEORDER_HTONLL_CONSTANT (   x)    BSLS_BYTEORDER_NTOHLL_CONSTANT(x)

◆ BSLS_BYTEORDER_HTONS

#define BSLS_BYTEORDER_HTONS (   x)    BSLS_BYTEORDER_NTOHS(x)

◆ BSLS_BYTEORDER_HTONS_CONSTANT

#define BSLS_BYTEORDER_HTONS_CONSTANT (   x)    BSLS_BYTEORDER_NTOHS_CONSTANT(x)

◆ BSLS_BYTEORDER_LE_TO_HOST

#define BSLS_BYTEORDER_LE_TO_HOST (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes(x)

◆ BSLS_BYTEORDER_LE_U16_TO_HOST

#define BSLS_BYTEORDER_LE_U16_TO_HOST (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes16(x)

◆ BSLS_BYTEORDER_LE_U32_TO_HOST

#define BSLS_BYTEORDER_LE_U32_TO_HOST (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes32(x)

◆ BSLS_BYTEORDER_LE_U64_TO_HOST

#define BSLS_BYTEORDER_LE_U64_TO_HOST (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes64(x)

◆ BSLS_BYTEORDER_NTOH

#define BSLS_BYTEORDER_NTOH (   x)    BloombergLP::bsls::ByteOrderUtil::swapBytes(x)

◆ BSLS_BYTEORDER_NTOHL

#define BSLS_BYTEORDER_NTOHL (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes32(x)

◆ BSLS_BYTEORDER_NTOHL_CONSTANT

#define BSLS_BYTEORDER_NTOHL_CONSTANT (   x)
Value:
((x) & 0x80 ? ~BSLS_BYTEORDER_NTOHL_CONSTANT_NO_MSB(~(x)) \
#define BSLS_BYTEORDER_NTOHL_CONSTANT_NO_MSB(x)
Definition bsls_byteorder.h:290

◆ BSLS_BYTEORDER_NTOHL_CONSTANT_NO_MSB

#define BSLS_BYTEORDER_NTOHL_CONSTANT_NO_MSB (   x)
Value:
((((x) >> 24) & 0x000000FF) | (((x) & 0x00FF0000) >> 8) \
/* note 0x7F */ | (((x) & 0x0000FF00) << 8) | (((x) & 0x0000007F) << 24))

◆ BSLS_BYTEORDER_NTOHLL

#define BSLS_BYTEORDER_NTOHLL (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes64(x)

◆ BSLS_BYTEORDER_NTOHLL_CONSTANT

#define BSLS_BYTEORDER_NTOHLL_CONSTANT (   x)
Value:
#define BSLS_BYTEORDER_NTOHLL_CONSTANT_NO_MSB(x)
Definition bsls_byteorder.h:297

◆ BSLS_BYTEORDER_NTOHLL_CONSTANT_NO_MSB

#define BSLS_BYTEORDER_NTOHLL_CONSTANT_NO_MSB (   x)
Value:
/* note 0x7F */ ((((x) & 0x000000000000007FLL) << 56) \
| (((x) & 0x000000000000FF00LL) << 40) \
| (((x) & 0x0000000000FF0000LL) << 24) \
| (((x) & 0x00000000FF000000LL) << 8) \
| (((x) & 0x000000FF00000000LL) >> 8) \
| (((x) & 0x0000FF0000000000LL) >> 24) \
| (((x) & 0x00FF000000000000LL) >> 40) \
| (((x) >> 56) & 0x00000000000000FFLL))

◆ BSLS_BYTEORDER_NTOHS

#define BSLS_BYTEORDER_NTOHS (   x)     BloombergLP::bsls::ByteOrderUtil::swapBytes16(x)

◆ BSLS_BYTEORDER_NTOHS_CONSTANT

#define BSLS_BYTEORDER_NTOHS_CONSTANT (   x)
Value:
static_cast<unsigned short>( \
(static_cast<unsigned short>(x) >> 8) | \
(static_cast<unsigned short>(x) << 8))