Skip to content

Instantly share code, notes, and snippets.

@rooklift
Last active May 8, 2021 17:41
Show Gist options
  • Save rooklift/d757da3c3e01e5d43e197a0215b654f7 to your computer and use it in GitHub Desktop.
Save rooklift/d757da3c3e01e5d43e197a0215b654f7 to your computer and use it in GitHub Desktop.

Due to a bunch of stupid issues in C with autocasting, sensible ways of parsing network bytes into something like an int might not work as expected, for which see here:

https://justine.lol/endian.html

The solution is something like this (mutatis mutandis if the incoming thing is little endian).

https://news.ycombinator.com/item?id=27086482

uint32_t read32be(const uint8_t *p) {
return (uint32_t)p[0] << 24
     | (uint32_t)p[1] << 16
     | (uint32_t)p[2] <<  8
     | (uint32_t)p[3];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment