networking · intermediate · ~15 min

Read a little-endian 16-bit sample

Decode a little-endian 16-bit value.

Challenge

I/Q samples are often stored little-endian (least-significant byte first), the opposite of network byte order. Decode a little-endian 16-bit value from two bytes.

Task

Implement int parse_le16(const unsigned char *b) that returns b[0] | (b[1] << 8)b[0] is the LOW byte and b[1] the high byte.

Input

A pointer b to at least 2 bytes.

Output

Returns the reassembled 16-bit value.

Example

{0x34, 0x12}   ->   0x1234
{0xFF, 0x00}   ->   255

Rules

  • Little-endian: the first byte is least significant (b[0] | (b[1] << 8)).

Input format

A pointer b to at least 2 bytes.

Output format

The 16-bit value b[0] | (b[1] << 8).

Constraints

Little-endian: b[0] is the low byte, b[1] the high byte.

Starter code

int parse_le16(const unsigned char *b) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.