LO: TO UNDERSTAND HOW TEXT IS REPRESENTED IN A COMPUTER SYSTEM
We now know that all data must be converted into 1s and 0s (ON or OFF) for your computer to understand. This means that when a key on your keyboard is pressed it must be converted into binary. The keyboards that we use on a daily basis use a character set called ASCII to do this. ASCII assigns each character on your keyboard a binary number. This is shown in the tables below….
ASCII CHARACTER SET
Copy the code below in to a new Python file.
# Lets the user select 's' to encode a message for sending # Or 'r' to translate a received message choice = input("Enter 's' for Send of 'r' for Receive: ") # Encoding a message for sending if choice == "s": message = input("Enter your message: ") binary_message = "" # Loops through the message character by character for i in message: # Converts each letter into it's denary ASCII code ascii_denary = ord(i) # Converts the denary ASCII code to 8-bit binary # and joins it all to gether as one long string binary_message += bin(ascii_denary)[2:].zfill(8) # Outputs the binary converted message print(binary_message) # Decoding a received message elif choice == "r": binary_message = input("Enter your message: ") message = "" # Loops through the binary message in steps of 8 for i in range(0,len(binary_message),8): # Picks out binary blocks of 8 binary = binary_message[i:i+8] # Converts them into base-2 (denary) number = int(binary, 2) # Converts the denary ASCII code into characters # and joins it all together as one long string message += chr(number) # Outputs the translated message print(message) else: exit()
Save the program in to your Programming folder, call it ASCII-Binary_Converter. Run the program, click on ‘s’ at the start to encode a message. Email the binary output to a friend who should then run the program again but this time select ‘r’ to translate the binary back to characters. Keep it clean!
Extension: Take a look at the code, see if you can work out how it works. Use the comments for guidance.