字节字符串和数字互转

x = 53523566
# 数字转为字节字符串
nbytes, rem = divmod(x.bit_length(), 8)
if rem:
    nbytes += 1
print(nbytes)
print(x.to_bytes(nbytes, 'little'))

# 字节字符串转为数字
data = b'n\xb40\x03'
print(int.from_bytes(data, 'little'))
posted @ 2020-12-18 23:19  公众号python学习开发  阅读(270)  评论(0编辑  收藏  举报