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)
{
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)
{
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;
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.
◆ 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
◆ BSLS_BYTEORDER_HTONL
◆ BSLS_BYTEORDER_HTONL_CONSTANT
◆ BSLS_BYTEORDER_HTONLL
◆ BSLS_BYTEORDER_HTONLL_CONSTANT
◆ BSLS_BYTEORDER_HTONS
◆ BSLS_BYTEORDER_HTONS_CONSTANT
◆ 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:
#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) \
| (((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: ((((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))