I need to convert an int, such as 123456, to bytes in the format b'123456'. I'm not doing an actual conversion to bytes however, I'm just changing the format to look like a byte and be interpreted as one. So I literally need to do the following:
10 = b'10'
20 = b'20'
30 = b'30'
and so. I can't convert to a string because I need the results to be in bytes, and I can't do an actual byte conversion because bytes([10]) == b'\n' (and not b'10').
Convert the int to a str then .encode into bytes:
>>> x = 123456
>>> bs = str(x).encode('ascii')
>>> bs
b'123456'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With