Perl's pack/unpack functions
John Hunter
jdhunter at nitace.bsd.uchicago.edu
Fri May 17 17:27:07 EDT 2002
>>>>> "Matthew" == Matthew Diephouse <fokke_wulf at hotmail.com> writes:
Matthew> I'm looking at learning Python, and the first app I want
Matthew> to write needs to be able to take a binary file and
Matthew> pack/unpack the data into hex form, and then into binary
Matthew> form. In Perl, you can do this using pack/unpack, but I
Matthew> don't see these functions in Python. So how would you do
Matthew> this in Python?
See the 'pack' function of module struct
http://python.org/doc/current/lib/module-struct.html
Here is an example that writes 2.5 GB of long integers in binary (I
wanted to test my python large file support build)
import struct
fd = open('test.out', 'w')
GB = 250000000L # each long is 4 bytes so 250000000 is 1 GB
for i in xrange( long(2.5*GB) ):
fd.write( struct.pack('L', i) )
More information about the Python-list
mailing list