title: Parsing network data for little and big endian
body: <p>How can I write a common datagram parser for both a big and little endian system? What I don't understand is how to pass bytes from a byte buffer 16 bits or 32 bits at a time...</p>
  <p>Suppose you have this datagram payload</p>
  <p><code>uint8_t datagram[8] = {0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8};</code></p>
  <p>And some protocol says</p>
  <ol>
  <li>Param a - 8 bits</li>
  <li>Param b - 16 bits (little endian)</li>
  <li>Param c - 8 bits</li>
  <li>Param d - 32 bits (little endian)</li>
  </ol>
  <p>So you want a common parser that would work on big and little endian machines</p>
  <pre><code>uint8_t param_a = datagram[0];
  uint16_t param_b = datagram[1]; // how to use ntohs here?
  uint8_t param_c = datagram[3]; 
  uint32_t param_d = datagram[4]; // how to use ntohl here?
  </code></pre>
  <p>Is it better to just cast to structs instead?</p>