On 07/11/2018 05:22, jlada...@itu.edu wrote: > On Tuesday, November 6, 2018 at 7:19:09 PM UTC-8, Terry Reedy wrote: >> On 11/6/2018 9:30 PM, j...y@it.u wrote: >> >>> b = i.to_bytes(1, "big") >>> >>> Is there another function which provides a more logical interface to this >>> straightforward task? >> >> Yes >> >>> 33 .to_bytes(1, 'big') >> b'!' >> >>> bytes((33,)) >> b'!' > > Thanks Terry, that's what I was looking for. > > I had tried using the bytes() constructor directly, and was getting a byte > array of zeros, of the length specified by the integer. That's in the > documentation, and it might be useful, but I haven't seen an obvious use > case, and it isn't what I wanted. Wrapping the integer in a tuple solves the > problem of it being interpreted as a length. >
A bytes is a sequence of 8-bit integers, which is why the constructor takes a sequence (or, well, iterable) of integers. >>> bytes([49,50,51]) b'123' -- https://mail.python.org/mailman/listinfo/python-list