1.Python提供了一个struct模块来解决bytes和其他二进制数据类型的转换。
struct的pack函数把任意数据类型变成bytes:
import struct
print(struct.pack('>I',10240099))
运行结果:
"C:\Program Files\Python36\python.exe" C:/Users/Administrator/PycharmProjects/Python全网练习/常用内置模块.py
b'\x00\x9c@c'
Process finished with exit code 0
2.unpack把bytes变成相应的数据类型:
import struct
print(struct.unpack('>I',b'\x00\x9c@c'))
运行结果:
"C:\Program Files\Python36\python.exe" C:/Users/Administrator/PycharmProjects/Python全网练习/常用内置模块.py
(10240099,)
Process finished with exit code 0