官网符号说明:https://docs.python.org/3/library/struct.html#format-characters



将一个32位的unsigned int 型数,拆分成4个的字节: pack

importstructprint(struct.pack('>I',10240099))#'>'表示转换成big-endian,即网络字节序,'I'表示'10240099'是4个字节的无符号整#形数d=struct.pack('<I',10240099)#'<'表示转换成little-endianprint(d)

运行结果:

b'\x00\x9c@c'b'c@\x9c\x00'


把相应的位数整合成一个数:unpack

a=struct.pack('>I',10240099)b=struct.unpack('>I',a)#将a的字节按big-endian转换成一个4字节表示的无符号整型数print(b)c=struct.unpack('>IH',b'\xf0\xf0\xf0\xf0\x80\x80')print(c)

运行结果:

(10240099,)(4042322160,32896)#(I,H)