Code

Text Becomes Bytes

Computers don't store letters. They store bytes: small numbers from 0 to 255. To save or send text, Python turns it into bytes using a recipe called UTF-8.

data = "Hi".encode("utf-8")
print(list(data))            # [72, 105]
print(data.decode("utf-8"))  # Hi
  • .encode("utf-8") turns text into bytes.
  • .decode("utf-8") turns bytes back into text.
  • bytes([72, 105]) builds bytes from a list of numbers.

Plain English letters take 1 byte each. Other characters need more: é takes 2 bytes and the emoji takes 4. UTF-8 can write every language on Earth this way.

Each byte is 8 bits

A byte is 8 bits, each on or off. format(72, "08b") shows them: 01001000. The starter draws each byte as a row of 8 pixels, with gold for 1 and dark for 0. That's your message in the computer's own language!

Assignment

  1. to_bytes(text) returns a list of the UTF-8 byte numbers of text. Use .encode("utf-8").
  2. from_bytes(numbers) turns a list of byte numbers back into text. Use bytes(...) and .decode("utf-8").

The drawing code is ready and uses your to_bytes.