title: fast binary data conversion in Python
body: <p>What is the fastest method for converting a binary data string to a numeric value in Python?</p>
  
  <p>I am using <code>struct.unpack_from()</code>, but am hitting a performance limit.  </p>
  
  <p>Context: an incoming stream is mixed binary and ASCII data.  The ASCII data conversion is done in C though ctypes.  Implementing the unpacking in C through ctypes yielded similar performance to unpack.  My guess is the call overhead was too much of a factor. I was hoping to find a native C-like coercion method (however un-Pythonic).  Most likely all of this code will need to move to C.</p>
  
  <p>The stream is in network byte order (big-endian) and the machine is little-endian.  An example conversion would be:</p>
  
  <pre><code>import struct
  network_stream = struct.pack('&gt;I', 0x12345678)
  (converted_int,) = struct.unpack_from('&gt;I', network_stream, 0) 
  </code></pre>
  
  <p>I am less concerned about handling the stream format, than the general case of binary conversion, and if there is even an alternative to <code>unpack</code>.  For example, <code>socket.ntohl()</code> requires an int, and <code>int()</code> won't convert a binary data string.</p>
  
  <p>Thanks for your suggestions!</p>