Is there a builtin function that converts ASCII to binary?
For example. converts 'P' to 01010000.
I'm using Python 2.6.6
How about two together?
bin(ord('P'))
# 0b1010000
0b means the number is expressed in binary (base 2). 0x would mean hexadecimal (base 16).Do you want to convert bytes or characters? There's a difference.
If you want bytes, then you can use
# Python 2.x
' '.join(bin(ord(x))[2:].zfill(8) for x in u'שלום, עולם!'.encode('UTF-8'))
# Python 3.x
' '.join(bin(x)[2:].zfill(8) for x in 'שלום, עולם!'.encode('UTF-8'))
The bin function converts an integer to binary. The [2:] strips the leading 0b. The .zfill(8) pads each byte to 8 bits.