Answers for "python ascii text to binary"

6

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'

#using `bytearray`
>>> ' '.join(format(x, 'b') for x in bytearray(st, 'utf-8'))
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'
Posted by: Guest on May-30-2020
2

text to binary python

a_string = "abc"

a_byte_array = bytearray(a_string, "utf8")

byte_list = []

for byte in a_byte_array:

    binary_representation = bin(byte)
    byte_list.append(binary_representation)

print(byte_list)

#Output ['0b1100001', '0b1100010', '0b1100011']
Posted by: Guest on October-28-2021

Python Answers by Framework

Browse Popular Code Answers by Language