unpack function in python

Neon Flash picture Neon Flash · Jul 13, 2013 · Viewed 32k times · Source

I am trying to understand the unpack function in Python and how it uses the format string.

I am using the format string, "I" which corresponds to unsigned int (size, 4 bytes) as an example.

As per the documentation, the unpack function will accept a string and convert it to a list of values based on the format string.

http://docs.python.org/2/library/struct.html

So, I used the input value as a string, "test" and here is the output:

>>> import struct
>>> input="test"
>>> l = struct.unpack("I", input)[0]
>>> print l
1953719668

I am trying to understand how the output value was derived from the input.

>>> from struct import *
>>> calcsize('I')
4

size of 'I' is 4 bytes. the string, "test" has 4 characters which is 4 bytes. I tried converting each character to its corresponding Hex ASCII value and storing it in little endian order but it does not match the output above.

Any help would be appreciated.

Answer

falsetru picture falsetru · Jul 13, 2013

Use 4s if you want unpack string as is.

>>> struct.unpack('4s', 'test')[0]
'test'

1953719668 is derived by: (little endian)

>>> ord('t') + (ord('e') << 8) + (ord('s') << 16) + (ord('t') << 24)
1953719668