# int list to str
lst1=list(map(lambda x:chr(x),[0x31,0x32]))
print("".join(lst1))
# int to hex_str
m = 0x666c61677b34396439313037376131616263623134663161396435343663383062653965667d
print(bytes.fromhex(hex(m)[2:]))
# int to bytes -- with zero padding
count = 0x12345678
a = count.to_bytes(4, "little")
b = count.to_bytes(4, "big")
a_bytes = int.from_bytes(a, "little")
b_bytes = int.from_bytes(b, "big")
print(a, b)
print(hex(a_bytes), hex(b_bytes))
# hex string to int to bytes
c_str = "0x12345678"[2:]
c_str_bytes = int(c_str, 16).to_bytes(4, "big")
# hex(int(c_str, 16)) == c_str
print(hex(int(c_str, 16)))
print(c_str_bytes)
Python list -- str -- int -- bytes 互转代码整理
最新推荐文章于 2023-05-08 07:09:12 发布