[Tutor] Need help with structure unpacking module
Nirmal Sakthi
yennes at gmail.com
Tue Mar 18 07:31:25 CET 2008
I am using module struct.unpack() to decode data from a binary file, so that
I can use the value in a calculation.
I have been able to extract an integer value.
>>>length = struct.unpack('i', '\x9c\x00\x00\x00')
>>>length = int(length[0])
>>>print length
156
I want to be able to extract a string.
I have tried,
>>>first = struct.unpack('s', '\x02\x00')
>>>first = str(first[0])
>>>print first
Traceback (most recent call last):
......
error: unpack requires a string argument of length 1
and,
>>>first = struct.unpack('cccc', '\x02\x00')
>>>first = str(first[0])
>>>print first
Traceback (most recent call last):
......
return o.unpack(s)
error: unpack requires a string argument of length 4
My desired result would be the string '0200'. Actually, I would like to be
able to invert the bytes to get '0002'.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080318/e4905db3/attachment.htm
More information about the Tutor
mailing list