Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 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位。