QUESTION

Text
Image

Hi there! I am stuck on this Python coding homework question. Below is a snip of the requirements and a snip of what was given in the editor.


Exercise 8.3.9: Text to Binary 5 points In this exercise, you'll write a program that encodes written text into binary data! As we've seen, every character can be represented by a string of 8 bits, or a byte. For example, the character ' $A$ ' maps to the decimal value of $65_{10}$ by the ASCII standard, and the binary value of $65_{10}$ is $01000001_{2}$ We can easily go from a character to the corresponding ASCII integer value using the Python method ord(letter). For example, the following code: text $=$ "Hello" letter $=\operatorname{text}[0: 1]$ numeric_value $=$ ord (letter $)$ print(letter $+"->++$ str(numeric_value)) text $=$ "Hello" letter $=\operatorname{text}[0: 1]$ numeric_value $=$ ord (letter $)$ print(letter + "-> + str(numeric_value)) Will print out \[ \mathrm{H} \longrightarrow 72 \] Using the decimal_to_binary function provided, write a function text_to_binary that takes a String of text, converts each character into its ASCII numerical value, converts each numerical value into its binary equivalent, and returns a string representing the translated text.
For example: text_to_binary("HI") Should return: "0100100001001001" To see why, let's examine this String: "HI" is made of 2 characters. The first character is ' $\mathrm{H}$ ' The ASCll value of ' $\mathrm{H}$ ' is 72 decimal_to_binary (72) returns "01001000" The next character is ' $l$ ' The ASCIl value of ' $I$ ' is 73 decimal_to_binary(73) returns "01001001" The resulting String is these two binary strings put together, resulting in "0100100001001001"
8.3.9: Text to Binary Save Submit "n" This program encodes user input into binary data! Your job is to write the textToBinary function "" " def text_to_binary(text): \# Write this method! \# For every character in the text, \# convert the character into its ASCII decimal encoding \# then convert that decimal value into its equivalent binary encoding $\#$ and combine each binary encoding to get the resulting binary string \# Converts a given decimal value into an 8 bit binary value def decimal_to_binary(decimal_value): binary_base $=2$ num_bits_desired $=8$ binäry_välue $=\operatorname{str}($ bin $($ decimal_value $))[2:]$ while len(binary_value) < num_bits_desired: binary_value $=" 0 "+$ binary_vā̄ue return binary_value text = input("Input the string you would like to encode: ") binary = text_to_binary (text) print(binary)

Public Answer

9E7OFX The First Answerer