吸毒的虫子 发表于 2015-4-24 09:48:17

python的struct使用注意

  python的struct模块可以让我们很方便的操作二进制数据,但是我们必须注意的是:我们在使用struct进行二进制操作的时候会发现,操作系统和硬件将影响程序是否正常运行。
  在
  Format = 'lllllfll'
f = open('test.dat','rb')
data = f.read(32)
s=[]
s.append(struct.unpack(Format,data))

32位下正常,64位下报:“struct.error: unpack requires a string argument of length 64”
同样是64位的操作系统,Windows和UNIX行为可能不太一样。UNIX上的long可能是64位,Windows可能就是32位。
  
  python语言的整形相当于C语言中的long型,在32位机器上位宽为32位,在64位系统上,整型的位宽为64位。
页: [1]
查看完整版本: python的struct使用注意