title: Javascript Typed Arrays and Endianness
body: <p>I'm using WebGL to render a binary encoded mesh file. The binary file is written out in big-endian format (I can verify this by opening the file in a hex editor, or viewing the network traffic using fiddler). When I try to read the binary response using a Float32Array or Int32Array, the binary is interpreted as little-endian and my values are wrong:</p>
  
  <pre><code>// Interpret first 32bits in buffer as an int
  var wrongValue = new Int32Array(binaryArrayBuffer)[0];
  </code></pre>
  
  <p>I can't find any references to the default endianness of typed arrays in <a href="http://www.khronos.org/registry/typedarray/specs/latest/">http://www.khronos.org/registry/typedarray/specs/latest/</a> so I'm wondering what's the deal? Should I assume that all binary data should be little-endian when reading using typed arrays?</p>
  
  <p>To get around the problem I can use a DataView object (discussed in the previous link) and call:</p>
  
  <pre><code>// Interpret first 32bits in buffer as an int
  var correctValue = new DataView(binaryArrayBuffer).getInt32(0);
  </code></pre>
  
  <p>The DataView functions such as "getInt32" read big-endian values by default.</p>
  
  <p>(Note: I've tested using Google Chrome 15 and Firefox 8 and they both behave the same way)</p>