# problem with logic in reading a binary file

Gary Herron gherron at islandtraining.com
Sat Mar 29 20:11:05 CET 2008

```Bryan.Fodness at gmail.com wrote:
> Hello,
>
> I am having trouble writing the code to read a binary string.  I would
> like to extract the values for use in a calculation.
>
> Any help would be great.
>

Without having looked at your code an any detail, may I humbly suggest
that you throw it all out and use the struct module:

http://docs.python.org/lib/module-struct.html

It is meant to solve this kind of problem, and it is quite easy to use.

Gary Herron

> Here is my function that takes in a string.
>
> def parseSequence(data, start):
>
>     group_num = data[start:start+2]
>     element_num = data[start+2:start+4]
>     vl_field = data[start+4:start+8]
>     length = struct.unpack('hh', vl_field)[0]
>     value = data[start+8:(start+8+length)]
>     pos = start+8+length
>     element = (group_num+element_num)
>
>     if element == '\xfe\xff\x00\xe0':
>         data = value
>
>         while start < length:
>             group_num = data[start:start+2]
>             element_num = data[start+2:start+4]
>             vl_field = data[start+4:start+8]
>             length = struct.unpack('hh', vl_field)[0]
>             value = data[start+8:(start+8+length)]
>             start = start+8+length
>             element = (group_num+element_num)
>
>             if element == '\xfe\xff\x00\xe0':
>                 data = value
>
>                 while start < length:
>                     group_num = data[start:start+2]
>                     element_num = data[start+2:start+4]
>                     vl_field = data[start+4:start+8]
>                     length = struct.unpack('hh', vl_field)[0]
>                     value = data[start+8:(start+8+length)]
>                     start = start+8+length
>                     element = (group_num+element_num)
>                     return element, start, value
>
>             else:
>                 return element, start, value
>
>     else:
>         return  element, pos, value
>
> And, here is a sample string (I have split up and indented for
> readability).  There is an identifier (\xfe\xff\x00\xe0) followed by
> the length of the nested values.
>
>
> '\xfe\xff\x00\xe0\x18\x02\x00\x00     -length=536
>      \n0q\x00\x02\x00\x00\x001
>      \n0x\x00\x02\x00\x00\x0010
>      \n0\x80\x00\x02\x00\x00\x004
>      \n0\xa0\x00\x02\x00\x00\x000
>      \x0c0\x04\x00\xe8\x01\x00\x00
>      \xfe\xff\x00\xe0p\x00\x00\x00     -length=112
>           \n0\x82\x002\x00\x00\x0042.9068704277562\\-392.3545926477\
> \189.182112099444
>           \n0\x84\x00\x0c\x00\x00\x008.9617062e-1
>           \n0\x86\x00\x10\x00\x00\x00127.378510918301
>           \x0c0\x06\x00\x02\x00\x00\x001
>      \xfe\xff\x00\xe0p\x00\x00\x00     -length=112
>           \n0\x82\x002\x00\x00\x0042.9068704277562\\-392.3545926477\
> \189.182112099444
>           \n0\x84\x00\x0c\x00\x00\x001.629998e-1
>           \n0\x86\x00\x10\x00\x00\x0023.159729257873
>           \x0c0\x06\x00\x02\x00\x00\x004
>      \xfe\xff\x00\xe0t\x00\x00\x00      -length=116
>           \n0\x82\x002\x00\x00\x0042.9068704277562\\-392.3545926477\
> \189.182112099444
>           \n0\x84\x00\x10\x00\x00\x001.26285318894435
>           \n0\x86\x00\x10\x00\x00\x00227.690980638769
>           \x0c0\x06\x00\x02\x00\x00\x003
>      \xfe\xff\x00\xe0t\x00\x00\x00      -length=116
>           \n0\x82\x002\x00\x00\x0042.9068704277562\\-392.3545926477\
> \189.182112099444
>           \n0\x84\x00\x10\x00\x00\x001.52797639111557
>           \n0\x86\x00\x10\x00\x00\x00263.433384670643
>           \x0c0\x06\x00\x02\x00\x00\x002 ')
>
>
>

```