title: How to switch byte order of binary data
body: <p>I have a binary file which I'm reading where some 2 byte values are stored in 'reverse' byte order (little endian?), eg.</p>
  
  <pre><code>1D 00 13 00 27  00 3B 00 45  00 31 00 4F
  </code></pre>
  
  <p>The original program that created these values stores them internally as shorts. Those values should correspond to: 29, 19, 39, 59, 69, 49, 79. I'm trying to read these values using python 2.6.5 (although this will probably be run on much older systems, eg 2.3 and 2.4).</p>
  
  <p>I've tried using</p>
  
  <pre><code>val1, val2, val3, val4, val5, val6, val7 = struct.unpack("1h 1h 1h 1h 1h 1h 1h", data)
  </code></pre>
  
  <p>and, of course, the values all come out wrong:</p>
  
  <pre><code>7427
  4864
  9984
  15104
  17664
  12544
  20224
  </code></pre>
  
  <p>After looking at the documentation for struct, I thought I'd be able to use something like</p>
  
  <pre><code>val1, ... = struct.unpack("!h !h ...
  </code></pre>
  
  <p>but when testing, I only got </p>
  
  <pre><code>struct.error: bad char in struct format
  </code></pre>
  
  <p>How can I unpack these values with the correct byte ordering? Am I stuck reading in the two bytes separately then reassembling them in the python code?</p>