<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  <title></title>
</head>
<body>
sorry for u1 type. here u1 means unsigned char.<br>
<br>
I'm just looking for a way to read it with struct module.<br>
I need a generic solution because there are a lot of different structures<br>
in my binary file and I wouldn't process each structure with<br>
a different piece of code. I would prefer use a different format and pass
it<br>
to struct module.<br>
<br>
My idea was to extend struct module in order to read sequence whose length<br>
is included in the binary file.<br>
For instance add a format caractere like ?X which means the value of the
unpacked<br>
data at index X<br>
<br>
<br>
Tim Peters a écrit:<br>
<blockquote type="cite"
 cite="midBIEJKCLHCIOIHAGOKOLHOECOFDAA.tim.one@comcast.net">
  <pre wrap="">[Franck Bui-Huu]
  </pre>
  <blockquote type="cite">
    <pre wrap="">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];
}
    </pre>
  </blockquote>
  <pre wrap=""><!---->
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)


  </pre>
  <blockquote type="cite">
    <pre wrap="">...
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.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
Doesn't seem worth the confusion or bother to me.


  </pre>
</blockquote>
<br>
</body>
</html>