python string binary code example
Example 1: char to binary python
>>> st = "hello world"
>>> ' '.join(format(ord(x), 'b') for x in st)
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'
>>> ' '.join(format(x, 'b') for x in bytearray(st, 'utf-8'))
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'
Example 2: format binary string python
x = 10
print(format(x, '#b'))
print(format(x, 'b'))
x= 0xF
print(format(x, 'b'))
print(f'{x:b}')
Example 3: format binary string python
>>> format(14, '#010b')
'0b00001110'