Struggling with struct.unpack() and "p" format specifier

From: Geoffrey (geskerrett_at_hotmail.com)
Date: 11/30/04


Date: 30 Nov 2004 06:57:21 -0800

Hope someone can help.
I am trying to read data from a file binary file and then unpack the
data into python variables. Some of the data is store like this;

xbuffer: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD'
# the above was printed using repr(xbuffer).
# Note that int(0x13) = 19 which is exactly the length of the visible
text
#

In the code I have the following statement;
x = st.unpack('>xxBBp',xbuffer)

This throws out the following error;

x = st.unpack('>xxBBp',xbuffer)
error: unpack str size does not match format

As I read the documentation the "p" format string seems to address
this situation, where the number bytes of the string to read is the
first byte of the stored value but I keep getting this error.

Am I missing something ?
Can the "p" format character be used to unpack this type of data ?

As I mentioned, I can parse the string and read it with multiple
statements, I am just looking for a more efficient solution.

Thanks.



Relevant Pages

  • Re: Struggling with struct.unpack() and "p" format specifier
    ... > I am trying to read data from a file binary file and then unpack the ... > data into python variables. ... the format string alone. ...
    (comp.lang.python)
  • Re: Unpack Expects the Wrong Number of Bytes
    ... unpack requires a string argument of length 8 ... So unpack expects 7 additional bytes when an integer is added to the ... It's adding pad bytes so that the format matches the equivilent C ... flag if endianness of the file format changes according to the machine ...
    (comp.lang.python)
  • Re: Struggling with struct.unpack() and "p" format specifier
    ... > I am trying to read data from a file binary file and then unpack the ... where the number bytes of the string to read is the ... Python 2.4rc1 or Python 2.3.3) does not act as documented on ...
    (comp.lang.python)
  • Re: Optimization help - reading out of /proc on Solaris
    ... string slicing and unpacking. ... we have a symbol mapping to an unpack format string segment ... #repeat the above with a new array instead of FIELDS and a new method name ... # # Print process table information for only pid 1001 ...
    (comp.lang.ruby)
  • Unpack When? Part II
    ... user interface or is it better to do all the work in Pick on the server and ... response to adding a string at the end of a previous string (ie like x += ... So I wonder if this would make a difference as to when to unpack? ... maybe we *should* unpack client-side. ...
    (comp.databases.pick)