networking · intermediate · ~15 min

htons via bytes

Serialise a 16-bit value to network order.

Challenge

Implement void htons_bytes(unsigned short x, unsigned char *out) writing x as two big-endian bytes (out[0] high, out[1] low) — host-independent.

Starter code

void htons_bytes(unsigned short x, unsigned char *out) {
    /* TODO */
}

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