title: Handling endianness when reading from a byte array in C?
body: <p>I have a byte array of data, which should be consistent cross-platform. Let's say I have a pointer, <code>unsigned char* data</code>, which points to some location inside my array, and I want to read 4 bytes into a variable. I would think that I could just to this:</p>
  
  <pre><code>uint32_t my_int = *data;
  </code></pre>
  
  <p>However, I realize that method doesn't account for endianness. For example, if my data was in big endian, would I have to do this to read it consistently?</p>
  
  <pre><code>uint32_t my_int = (data[0] &lt;&lt; 3) + (data[1] &lt;&lt; 2) + (data[2] &lt;&lt; 1) + data[3];
  </code></pre>
  
  <p>Likewise, do I have to make the same checks when writing this data with <code>fwrite</code>? For example, if I wrote that same data to a file with this code:</p>
  
  <pre><code>fwrite(&amp;my_int, sizeof(my_int), 1, fh);
  </code></pre>
  
  <p>Would the resulting data have any known endianness? Or would it be dependent upon architecture? If so, what's the simplest way to do these reads and writes and enforce a particular endianness on <em>all</em> platforms?</p>