title: Parsing binary data into ctypes Structure object via readinto()
body: <p>I'm trying to handle a binary format, following the example here:</p>
  
  <p><a href="http://dabeaz.blogspot.jp/2009/08/python-binary-io-handling.html" rel="noreferrer">http://dabeaz.blogspot.jp/2009/08/python-binary-io-handling.html</a></p>
  
  <pre><code>&gt;&gt;&gt; from ctypes import *
  &gt;&gt;&gt; class Point(Structure):
  &gt;&gt;&gt;     _fields_ = [ ('x',c_double), ('y',c_double), ('z',c_double) ]
  &gt;&gt;&gt;
  &gt;&gt;&gt; g = open("foo","rb") # point structure data
  &gt;&gt;&gt; q = Point()
  &gt;&gt;&gt; g.readinto(q)
  24
  &gt;&gt;&gt; q.x
  2.0
  </code></pre>
  
  <p>I've defined a Structure of my header and I'm trying to read data into my structure, but I'm having some difficulty.
  My structure is like this:</p>
  
  <pre><code>class BinaryHeader(BigEndianStructure):
      _fields_ = [
                  ("sequence_number_4bytes", c_uint),
                  ("ascii_text_32bytes", c_char),
                  ("timestamp_4bytes", c_uint),
                  ("more_funky_numbers_7bytes", c_uint, 56),
                  ("some_flags_1byte", c_byte),
                  ("other_flags_1byte", c_byte),
                  ("payload_length_2bytes", c_ushort),
  
                  ] 
  </code></pre>
  
  <p>The <a href="http://docs.python.org/2/library/ctypes.html#ctypes.Structure._fields_" rel="noreferrer">ctypes documentation</a> says:</p>
  
  <blockquote>
    <p>For integer type fields like c_int, a third optional item can be
    given. It must be a small positive integer defining the bit width of
    the field.</p>
  </blockquote>
  
  <p>So for <code>("more_funky_numbers_7bytes", c_uint, 56),</code> I've tried to define the field as a 7 byte field, but I'm getting the error:</p>
  
  <blockquote>
    <p>ValueError: number of bits invalid for bit field</p>
  </blockquote>
  
  <p>So my first problem, is how can I define a 7 byte int field?</p>
  
  <p>Then If I skip that problem and comment out the "more_funky_numbers_7bytes" field, the resulting data get's loaded in.. but as expected only 1 character is loaded into "ascii_text_32bytes".  And for some reason returns <code>16</code> which I assume is the calculated number of bytes it read into the structure... but If I'm commenting out my "funky number" field and ""ascii_text_32bytes" is only giving one char (1 byte), shouldn't that be 13, not 16???</p>
  
  <p>Then I tried breaking out the char field into a separate structure, and reference that from within my Header structure. But that's not working either...</p>
  
  <pre><code>class StupidStaticCharField(BigEndianStructure):
      _fields_ = [
                  ("ascii_text_1", c_byte),
                  ("ascii_text_2", c_byte),
                  ("ascii_text_3", c_byte),
                  ("ascii_text_4", c_byte),
                  ("ascii_text_5", c_byte),
                  ("ascii_text_6", c_byte),
                  ("ascii_text_7", c_byte),
                  ("ascii_text_8", c_byte),
                  ("ascii_text_9", c_byte),
                  ("ascii_text_10", c_byte),
                  ("ascii_text_11", c_byte),
                  .
                  .
                  .
                  ]
  
  class BinaryHeader(BigEndianStructure):
      _fields_ = [
                  ("sequence_number_4bytes", c_uint),
                  ("ascii_text_32bytes", StupidStaticCharField),
                  ("timestamp_4bytes", c_uint),
                  #("more_funky_numbers_7bytes", c_uint, 56),
                  ("some_flags_1byte", c_ushort),
                  ("other_flags_1byte", c_ushort),
                  ("payload_length_2bytes", c_ushort),
  
                  ] 
  </code></pre>
  
  <p>So, any ideas how to:</p>
  
  <ol>
  <li>Define a 7 byte field (which I'll need to decode using a defined function)</li>
  <li>Define a static char field of 32 bytes</li>
  </ol>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've found a structure that seems to work...</p>
  
  <pre><code>class BinaryHeader(BigEndianStructure):
      _fields_ = [
                  ("sequence_number_4bytes", c_uint),
                  ("ascii_text_32bytes", c_char * 32),
                  ("timestamp_4bytes", c_uint),
                  ("more_funky_numbers_7bytes", c_byte * 7),
                  ("some_flags_1byte", c_byte),
                  ("other_flags_1byte", c_byte),
                  ("payload_length_2bytes", c_ushort),
  
                  ]  
  </code></pre>
  
  <p>Now, however, my remaining question is, why when use <a href="http://docs.python.org/2/library/io.html#io.BufferedIOBase.readinto" rel="noreferrer"><code>.readinto()</code></a>:</p>
  
  <pre><code>f = open(binaryfile, "rb")
  
  mystruct = BinaryHeader()
  f.readinto(mystruct)
  </code></pre>
  
  <p>It's returning <code>52</code> and not the expected, <code>51</code>.  Where is that extra byte coming from, and where does it go?</p>
  
  <p><strong>UPDATE 2</strong>
  For those interested here's an <a href="http://docs.python.org/2/library/struct.html#examples" rel="noreferrer">example</a> of an alternative <code>struct</code> method to read values into a namedtuple mentioned by eryksun:</p>
  
  <pre><code>&gt;&gt;&gt; record = 'raymond   \x32\x12\x08\x01\x08'
  &gt;&gt;&gt; name, serialnum, school, gradelevel = unpack('&lt;10sHHb', record)
  
  &gt;&gt;&gt; from collections import namedtuple
  &gt;&gt;&gt; Student = namedtuple('Student', 'name serialnum school gradelevel')
  &gt;&gt;&gt; Student._make(unpack('&lt;10sHHb', record))
  Student(name='raymond   ', serialnum=4658, school=264, gradelevel=8)
  </code></pre>