Hacker News new | ask | show | jobs
by pwdisswordfish8 1835 days ago
Better yet is using wrappers for char arrays paired with helper functions:

    typedef struct { unsigned char data[2]; } u16be_t;

    inline static uint16_t get_u16be(const u16be_t *p) {
        uint16_t result = 0;
        result |= (uint16_t) p->data[0] << 8;
        result |= (uint16_t) p->data[1];
        return result;
    }

    inline static void put_u16be(u16be_t *p, uint16_t value) {
        p->data[0] = value >> 8;
        p->data[1] = value;
    }
Perfectly portable (as long as CHAR_BIT = 8), type-safe, and on modern compilers it generates no overhead over direct memory accesses.