Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top