basics · intermediate · ~15 min
Reconstruct an integer from bytes on the wire.
Assemble a 32-bit integer from 4 bytes in network (big-endian) order.
Network byte order is big-endian: the most significant byte comes first. Implement uint32_t read_be32(const uint8_t *b) that reads 4 bytes from b and assembles them into a 32-bit value, with b[0] as the most significant byte. So b[0] shifts left by 24, b[1] by 16, b[2] by 8, and b[3] stays in the low byte. No main — the grader calls it.
A pointer b to (at least) 4 bytes.
Returns the reconstructed 32-bit value, as a uint32_t.
{0x11,0x22,0x33,0x44} -> 0x11223344
{0xFF,0x00,0x00,0x01} -> 0xFF000001
{0,0,0,0} -> 0
uint32_t before shifting so high bits aren't lost.A pointer b to at least 4 bytes.
The big-endian 32-bit value, as a uint32_t.
b[0] is the most significant byte.
#include <stdint.h>
uint32_t read_be32(const uint8_t *b) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.