struct module...

Franck Bui-Huu Franck.BUI-HUU at gemplus.com
Fri Mar 21 17:40:22 CET 2003


sorry for u1 type. here u1 means unsigned char.

I'm just looking for a way to read it with struct module.
I need a generic solution because there are a lot of different structures
in my binary file and I wouldn't process each structure with
a different piece of code. I would prefer use a different format and pass it
to struct module.

My idea was to extend struct module in order to read sequence whose length
is included in the binary file.
For instance add a format caractere like ?X which means the value of the 
unpacked
data at index X


Tim Peters a écrit:

>[Franck Bui-Huu]
>  
>
>>I'm trying to read from a binary file data.
>>These data have been written by a C soft using structure like
>>struct xxx {
>>    u1 length;
>>    u1 array[length];
>>}
>>    
>>
>
>Picturing a struct isn't really helpful here, in part because C structs
>don't have variable length.  How the file was actually written should
>suggest how to read it back in.  I don't know what "u1" means here, but
>assuming length is an int and array is full of doubles, and the data was
>written in big-endian ("network") order and without pad bytes, this kind of
>reading code is likely appropriate:
>
>    data = f.read(4)
>    if len(data) != 4:
>        raise SomeException
>    n = struct.unpack(">i", data)[0]
>
>    data = f.read(8*n)
>    if len(data) != 8*n:
>        raise SomeException
>    array = struct.unpack(">%dd" % n, data)
>
>OTOH, if u1 here means unsigned byte, life would be easier if you skipped
>the struct module:
>
>    data = f.read(1)
>    if len(data) != 1:
>        raise SomeException
>    n = ord(data)
>
>    data = f.read(n)
>    if len(data) != n:
>        raise SomeException
>    array = map(ord, data)
>
>
>  
>
>>...
>>did someone extend struct module to manage not fixed lengh of
>>array. We could imagine a format for struct xxx like:
>>'B?(1)c' where ?(1) means the value of the first unpcked data.
>>    
>>
>
>Doesn't seem worth the confusion or bother to me.
>
>
>  
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030321/b07abcd7/attachment.html>


More information about the Python-list mailing list