basics · intermediate · ~15 min

Read a big-endian uint32

Reconstruct an integer from bytes on the wire.

Challenge

Assemble a 32-bit integer from 4 bytes in network (big-endian) order.

Task

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.

Input

A pointer b to (at least) 4 bytes.

Output

Returns the reconstructed 32-bit value, as a uint32_t.

Example

{0x11,0x22,0x33,0x44}   ->   0x11223344
{0xFF,0x00,0x00,0x01}   ->   0xFF000001
{0,0,0,0}               ->   0

Edge cases

  • All-zero bytes give 0.

Rules

  • Cast each byte to uint32_t before shifting so high bits aren't lost.

Input format

A pointer b to at least 4 bytes.

Output format

The big-endian 32-bit value, as a uint32_t.

Constraints

b[0] is the most significant byte.

Starter code

#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.