Python3 如何将一个以字节表示的大数转换成整数?
Python 3 how to convert a large number expressed as bytes into an integer?
我有:
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
我有:
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