文档上的例子:
A basic example of packing/unpacking three integers:
>>>
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>> calcsize('hhl')
8
我电脑上的实际结果:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
'\x01\x00\x02\x00\x03\x00\x00\x00'
>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(256, 512, 50331648)
>>> calcsize('hhl')
8
>>>
A basic example of packing/unpacking three integers:
>>>
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>> calcsize('hhl')
8
我电脑上的实际结果:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
'\x01\x00\x02\x00\x03\x00\x00\x00'
>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(256, 512, 50331648)
>>> calcsize('hhl')
8
>>>