int.to_bytes() for a single byte
bob gailer
bgailer at gmail.com
Tue Nov 6 21:43:50 EST 2018
On 11/6/2018 9:30 PM, jladasky at itu.edu wrote:
> I'm using Python 3.6. I have a need to convert several small integers into single bytes. As far as I can tell from reading through the Python docs, the correct way to accomplish this task is:
>
> b = i.to_bytes(1, "big")
>
> This seems to work, but I find it cumbersome. I have to supply the byteorder argument to prevent a TypeError. However, byte order doesn't matter if I'm only generating a single byte. Whether I choose "big" or "little" I should get the same result. Is there another function which provides a more logical interface to this straightforward task?
Take a look at the struct module.
Bob Gailer
More information about the Python-list
mailing list