Big Binary Code into File in Python
<p>I have been working on a program and I have been trying to convert a big binary file (As a string) and pack it into a file. I have tried for days to make such thing possible. Here is the code I had written to pack the large binary string. </p>

<pre><code>binaryRecieved="11001010101....(Shortened)"
f=open(fileName,'wb')
m=long(binaryRecieved,2)
struct.pack('i',m)
f.write(struct.pack('i',m))
f.close()
quit()
</code></pre>

<p>I am left with the error</p>

<pre><code>struct.pack('i',x)
struct.error: integer out of range for 'i' format code
</code></pre>

<p>My integer is out of range, so I was wondering if there is a different way of going about with this.</p>

<p>Thanks</p>

<p>Convert your bit string to a byte string: see for example this question <a href="http://stackoverflow.com/questions/28370991/converting-bits-to-bytes-in-python">Converting bits to bytes in Python</a>. Then pack the bytes with <code>struct.pack('c', bytestring)</code></p>

python file binary