[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Struct.Pack and Binary files

PurpleServerMonkey

1/28/2008 1:24:00 AM

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.
2 Answers

Grant Edwards

1/28/2008 2:49:00 AM

0

On 2008-01-28, PurpleServerMonkey <PurpleServerMonkey@gmail.com> wrote:
> 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.

--
Grant

PurpleServerMonkey

1/28/2008 9:45:00 AM

0

On Jan 28, 1:48 pm, Grant Edwards <gra...@visi.com> wrote:
> On 2008-01-28, PurpleServerMonkey <PurpleServerMon...@gmail.com> wrote:
>
> > 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.
>
> --
> Grant

Thanks Grant,

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