我有:
n = 257
a = n.to_bytes(2, 'little')
a = b'\x01\x01'
如何将其转换回 257
另外,有没有办法在不指定多少字节的情况下显示to_bytes
?
最佳答案
使用互补的int.from_bytes
并再次指定字节顺序。
>>> n = 257
>>> n_bytes = n.to_bytes(2, "little")
>>> n_again = int.from_bytes(n_bytes, "little")
>>> n_again == n
True
关于Python 3如何将以字节表示的大量数字转换为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126236/