title: Python struct error
body: <p>I'm trying to design a system to react to different binary flags.</p>
  
  <pre><code>0 = Error
  1 = Okay
  2 = Logging
  3 = Number
  </code></pre>
  
  <p>The sequence of this data represents a unique ID to reference the work, the flag and the number. Everything works, except the number flag. This is what I get...</p>
  
  <pre><code>&gt;&gt;&gt; import struct
  &gt;&gt;&gt; data = (1234, 3, 12345678)
  &gt;&gt;&gt; bin = struct.pack('QHL', *data)
  &gt;&gt;&gt; print(bin)
  b'\xd2\x04\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00Na\xbc\x00\x00\x00\x00\x00'
  &gt;&gt;&gt; result = struct.unpack_from('QH', bin, 0)
  &gt;&gt;&gt; print(result)
  (1234, 3)
  &gt;&gt;&gt; offset = struct.calcsize('QH')
  &gt;&gt;&gt; result += struct.unpack_from('L', bin, offset)
  &gt;&gt;&gt; print(result)
  (1234, 3, 7011541669862440960)
  </code></pre>
  
  <p>A long should be plenty big to represent the number <code>12345678</code>, but why is it incorrectly unpacked?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>When I try to pack them separately, it looks like struct is adding too many null bytes between the flag and the long.</p>
  
  <pre><code>&gt;&gt;&gt; import struct
  &gt;&gt;&gt; struct.pack('QH', 1234, 3)
  b'\xd2\x04\x00\x00\x00\x00\x00\x00\x03\x00'
  &gt;&gt;&gt; struct.pack('L', 12345678)
  b'Na\xbc\x00\x00\x00\x00\x00'
  </code></pre>
  
  <p>I can reproduce this error by adding padding before the long.</p>
  
  <pre><code>&gt;&gt;&gt; struct.unpack('L', struct.pack('L', 12345678))
  (12345678,)
  &gt;&gt;&gt; struct.unpack('xL', struct.pack('xL', 12345678))
  (12345678,)
  &gt;&gt;&gt; struct.pack('xL', 12345678)
  b'\x00\x00\x00\x00\x00\x00\x00\x00Na\xbc\x00\x00\x00\x00\x00'
  </code></pre>
  
  <p><strong>Potential fix?</strong></p>
  
  <p>When I use little-endian order, the problem seems to correct itself and make the binary string shorter. Since this is destined for a SSL wrapped TCP socket, that's a win win, right? Keeping bandwidth low is generally good, yes?</p>
  
  <pre><code>&gt;&gt;&gt; import struct
  &gt;&gt;&gt; data = (1234, 3, 12345678)
  &gt;&gt;&gt; bin = struct.pack('&lt;QHL', *data)
  &gt;&gt;&gt; print(bin)
  b'\xd2\x04\x00\x00\x00\x00\x00\x00\x03\x00Na\xbc\x00'
  &gt;&gt;&gt; result = struct.unpack_from('&lt;QH', bin, 0)
  &gt;&gt;&gt; print(result)
  (1234, 3)
  &gt;&gt;&gt; offset = struct.calcsize('&lt;QH')
  &gt;&gt;&gt; result += struct.unpack_from('&lt;L', bin, offset)
  &gt;&gt;&gt; print(result)
  (1234, 3, 12345678)
  </code></pre>
  
  <p>Why does this happen? I am perplexed.</p>