EmbedwrightArticlesLesson guideFree lessonsThe course · $79

Decode little-endian fields without pointer casts

By Chinmay Gupta · Updated 2026-12-01
Short answer. A protocol defines its own byte order. Decode bytes into an unsigned value with shifts and bitwise operations instead of assuming the host object representation matches the wire format.

A binary message may arrive in an array with no guarantee that a multi-byte field is naturally aligned. Casting a pointer to that array to a wider integer pointer can introduce alignment and effective-type problems, and it still assumes the machine byte order matches the format. The protocol should define the layout independently of the compiler.

The contract

For a two-byte little-endian field, the first octet contributes the low eight bits and the second contributes the high eight bits. Check that both are present before reading. The arithmetic below converts the high byte to a sufficiently wide unsigned type before shifting. The result is then narrowed to the intended field width.

A small executable example

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <assert.h>
#include <stdio.h>
static bool read_le16(const uint8_t *p, size_t n, uint16_t *out) {
    if (n < 2) return false;
    *out = (uint16_t)((uint32_t)p[0] | ((uint32_t)p[1] << 8));
    return true;
}
int main(void) {
    const uint8_t p[] = {0x34, 0x12};
    uint16_t v = 0;
    assert(!read_le16(p, 1, &v));
    assert(read_le16(p, 2, &v) && v == 0x1234);
    printf("%04x\n", (unsigned)v);
}
Real output
1234

Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.

Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1

What the example does not establish

The function assumes its input pointer refers to at least n readable bytes and the output pointer is valid. The length check does not validate arbitrary pointers. If offsets come from the message itself, validate the offset before forming a derived pointer and compare the remaining length rather than adding potentially overflowing quantities. A byte decoder is one part of a parser, not the whole parser.

Checks to add in your own project

For a test corpus, include the same bytes interpreted in the opposite byte order, all-zero and all-one fields, and a truncated input. Test against protocol fixtures captured or independently generated. On target hardware, separately measure parsing cost if this code runs on a hot path. Correctness should not depend on the alignment selected by a host allocator.

The course goes further on this topic. Lesson 3 of DSA for Firmware, “The 12-byte packet”, adds interactive labs and C exercises with tests that you run on your own machine. Read the lesson guide · See the course
Want the full path from “I know C” to “I can choose the right data structure under a RAM budget”?
DSA for Firmware teaches it one real bug at a time.
  • 28 lessons, each starting from a real firmware bug.
  • 110 C exercises with tests, and a reference solution for every one.
  • Lessons 1 and 2 are free. No sign-up.
Start Lesson 1 freeSee the full course · $79

Get the next article by email

One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.

Chinmay Gupta
Chinmay Gupta is a firmware engineer with more than eight years of experience writing and debugging embedded C. He builds Embedwright's DSA for Firmware course.