big_endian
Overview
Endianness is the order of bytes of a word of digital data in computer memory. The two main types of endianness is Big Endian (BE) and Little Endian (LE). In Big Endian, the most significant bit (MSB) is stored first (i.e. in the lowest memory address).
For example, on a big-endian computer, Consider an integer 0x12345678 stored in a 32-bit big-endian system. The bytes would be arranged in memory as:
Address |
Value |
---|---|
100 |
0x12 |
101 |
0x34 |
102 |
0x56 |
103 |
0x78 |
Usage
The “big_endian” stores the data in an array of bytes. The size of the array is determined by the size of the data type being represented. For example, a 16-bit integer, the “big_endian” will have an array of 2 bytes to store the data in “big_endian” format.
Example
The example shows reading data in big-endian format from buffer array.
uint8_t const buffer[] = {0xAB, 0x12};
EXPECT_EQ(0xAB12U, ::estd::read_be<uint16_t>(&buffer[0]));
uint8_t buffer[4] = {0};
::estd::write_be<uint32_t>(&buffer[0], 0x12345678);
EXPECT_THAT(buffer, ElementsAre(0x12, 0x34, 0x56, 0x78));