[英]Is there some equivalent function in python for pack from php?
我一般不熟悉二进制运算,因此对于PHP我是通过示例完成的,但是找不到适用于python的任何东西...我需要python中的某些东西,例如php pack('H*'
和pack('a*'
。
您正在寻找python struct
模块。
>>> import struct
>>> struct.pack("<H", 5) # Little endian short, 2 bytes.
'\x05\x00'
请注意, php.pack()
H
与struct H
。
如果要将整数转换为十六进制:
>>> import random
>>> hex(random.randint(0, 100))
'0x4c'
但是在python中,十六进制是字符串,所以我不确定这是否是您要的。 如果要实际将random.randint()
的输出打包为二进制数据,请将值打包为某个整数。 至少可以使用格式字符B, H, I, L, Q
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.