Struct.Pack and Binary files

P

PurpleServerMonkey

Having trouble working out an appropriate format string for packing a
binary file.

The below is something I use for ASCII files but now I need something
equivalent for working with binary files i.e jpg, zips etc.

fileHandle = open("test.txt")

while loop:
fileBuffer = fileHandle.read(512)
format = "!hh%dc" % len(fileBuffer)
outdata = struct.pack(format, *fileBuffer)
clientSocket.sendto(outdata, DestAddress)

I've reused the basic structure below for a binary file, the issue I'm
having is working out the correct format string. At first I thought a
float or double would have been the one to use but the interpreter
complains about invalid types being passed.

fileHandle = open("test.zip", "rb")

while loop:
fileBuffer = fileHandle.read(512)
format = "!hh%dd" % len(fileBuffer)
outdata = struct.pack(format, *fileBuffer)
clientSocket.sendto(outdata, DestAddress)

If someone could shed some light on the problem it would be
appreciated, I'm clearly missing something fairly obvious.

Thanks in advance.
 
G

Grant Edwards

Having trouble working out an appropriate format string for packing a
binary file.

The below is something I use for ASCII files but now I need something
equivalent for working with binary files i.e jpg, zips etc.

fileHandle = open("test.txt")

while loop:
fileBuffer = fileHandle.read(512)
format = "!hh%dc" % len(fileBuffer)
outdata = struct.pack(format, *fileBuffer)

Assuming fileBuffer has 512 bytes in it, your format string is
going to be "!hh512c". When struct.pack sees that, it expects
514 values to pack. You're only passing it 512. What data do
you want to be packed into the two "h" fields?

You're also wasting a lot of CPU time unpacking and then
repacking the 512 bytes in filebufffer. I suspect what you
want is:

val1 = ???
val2 = ???
outdata = format.struct("!hh", val1, val2) + fileBuffer

I don't know what val1 and val2 are supposed to be, since the
values that are to be packed as the two "h" fields seems to be
missing from your example code.
 
P

PurpleServerMonkey

Assuming fileBuffer has 512 bytes in it, your format string is
going to be "!hh512c". When struct.pack sees that, it expects
514 values to pack. You're only passing it 512. What data do
you want to be packed into the two "h" fields?

You're also wasting a lot of CPU time unpacking and then
repacking the 512 bytes in filebufffer. I suspect what you
want is:

val1 = ???
val2 = ???
outdata = format.struct("!hh", val1, val2) + fileBuffer

I don't know what val1 and val2 are supposed to be, since the
values that are to be packed as the two "h" fields seems to be
missing from your example code.

Thanks Grant,

Yeah I left out the two 'h' values by mistake but your suggestion
worked out really well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top