Using this code to take a string and convert it to binary:
bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in 'hello'), 0))
this outputs:
0b110100001100101011011000110110001101111
Which, if I put it into this site (on the right hand site) I get my message of hello back. I’m wondering what method it uses. I know I could splice apart the string of binary into 8’s and then match it to the corresponding value to bin(ord(character)) or some other way. Really looking for something simpler.
For ASCII characters in the range
[ -~]on Python 2:In reverse:
In Python 3.2+:
In reverse:
To support all Unicode characters in Python 3:
Here’s single-source Python 2/3 compatible version:
Example