8.3.9 Text to Binary Python
![839 Text to Binary Python class=](https://us-static.z-dn.net/files/dd8/c7fd128107ed58a2a8c06f0cec4fbcee.png)
Using the knowledge in computational language in python it is possible to write a code that write the binary code
#function that will convert the text to binary
def text_to_binary(text):
#variable to store the converted binary value
binary_text = ""
#iterate for each character in the text
for i in text:
#get the ASCII value of that character
ascii = ord(i)
#call the function to get the value of the
#current ascii number and append it to the
#binary_text
binary_text += decimal_to_binary(ascii)
#return the final converted binary value
return binary_text
def decimal_to_binary(decimal_value):
binary_base = 2
num_bits_desired = 8
binary_value = str(bin(decimal_value))[2:]
while len(binary_value) < num_bits_desired:
binary_value = "0" + binary_value
return binary_value
text = input("Input the string you would like to encode: ")
binary = text_to_binary(text)
print(binary)
See more about python at brainly.com/question/18502436
#SPJ1