chr() equivalent returning a bytes object, in py3k
Solution 1:
Try the following:
b = bytes([x])
For example:
>>> bytes([255])
b'\xff'
Solution 2:
Consider using bytearray((255,)) which works the same in Python2 and Python3. In both Python generations the resulting bytearray-object can be converted to a bytes(obj) which is an alias for a str() in Python2 and real bytes() in Python3.
# Python2
>>> x = bytearray((32,33))
>>> x
bytearray(b' !')
>>> bytes(x)
' !'
# Python3
>>> x = bytearray((32,33))
>>> x
bytearray(b' !')
>>> bytes(x)
b' !'
Solution 3:
In case you want to write Python 2/3 compatible code, use six.int2byte
Solution 4:
>>> import struct
>>> struct.pack('B', 10)
b'\n'
>>> import functools
>>> bchr = functools.partial(struct.pack, 'B')
>>> bchr(10)
b'\n'