python 字节转换:struct
官网符号说明: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)
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。