int.to_bytes() for a single byte

jladasky at itu.edu jladasky at itu.edu
Tue Nov 6 21:30:05 EST 2018


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?

Thanks for any suggestions.



More information about the Python-list mailing list