Quick Links: |
Provide byte-order manipulation macros. More...
BSLS_BYTEORDER_HTON(x) | Convert value from host to network order |
BSLS_BYTEORDER_HTONS(x) | Convert 16-bit value from host to network order |
BSLS_BYTEORDER_HTONL(x) | Convert 32-bit value from host to network order |
BSLS_BYTEORDER_HTONLL(x) | Convert 64-bit value from host to network order |
BSLS_BYTEORDER_NTOH(x) | Convert value from network to host order |
BSLS_BYTEORDER_NTOHS(x) | Convert 16-bit value from network to host order |
BSLS_BYTEORDER_NTOHL(x) | Convert 32-bit value from network to host order |
BSLS_BYTEORDER_NTOHLL(x) | Convert 64-bit value from network to host order |
htonl
, htons
, ntohl
, and ntohs
functions, and which do not require including any system header files: BSLS_BYTEORDER_HTON(x) BSLS_BYTEORDER_HTONS(x) BSLS_BYTEORDER_HTONL(x) BSLS_BYTEORDER_HTONLL(x) BSLS_BYTEORDER_NTOH(x) BSLS_BYTEORDER_NTOHS(x) BSLS_BYTEORDER_NTOHL(x) BSLS_BYTEORDER_NTOHLL(x)
short
), 32-bit (int
, not long
), and 64-bit (long long
) values, respectively. 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. 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); }
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): "); printHex(BSLS_BYTEORDER_LE_U16_TO_HOST(x)); printf("\nLE to Host(y): "); printHex(BSLS_BYTEORDER_LE_U32_TO_HOST(y)); printf("\nLE to Host(z): "); printHex(BSLS_BYTEORDER_LE_U64_TO_HOST(z)); printf("\nBE to Host(x): "); printHex(BSLS_BYTEORDER_BE_U16_TO_HOST(x)); printf("\nBE to Host(y): "); printHex(BSLS_BYTEORDER_BE_U32_TO_HOST(y)); printf("\nBE to Host(z): "); printHex(BSLS_BYTEORDER_BE_U64_TO_HOST(z));
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
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