Reputation: 13206
I have:
n = 257
a = n.to_bytes(2, 'little')
a = b'\x01\x01'
How can i convert this back into 257
Also, is there any way to show to_bytes
without specifying how many bytes?
Upvotes: 2
Views: 135
Reputation: 1069
Use the complementary int.from_bytes
and specify the byteorder again.
>>> n = 257
>>> n_bytes = n.to_bytes(2, "little")
>>> n_again = int.from_bytes(n_bytes, "little")
>>> n_again == n
True
Upvotes: 3