What is the equivalent of chr(x) for bytes in python 3?

Baptiste Lepilleur baptiste.lepilleur at gmail.com
Sat Dec 26 05:50:08 EST 2009


Hi,
I'm looking for the equivalent of the built-in chr(x) function that would
return a bytes type taking an integer as parameter.

The easiest way I found to do this is the function below, but there must be
some simpler way to do that and I must be overlooking something fairly
obvious...

def byte( x ):
...     import io
...     s = io.BytesIO()
...     s.write( bytearray( (x,) ) )
...     return s.getvalue()
...
>>> byte( 12 )
b'\x0c'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091226/7cb09c96/attachment-0001.html>


More information about the Python-list mailing list