CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 10 / BEGINNER PYTHON

Bits, bytes and secret-looking messages

Representation is how information is written down. Hexadecimal and Base64 encode data; they do not make it secret.

The idea

The same number can be written in different bases: decimal 15 and hexadecimal F name the same quantity. A byte contains eight bits, and two hexadecimal digits can describe one byte. To turn text into bytes, you also need a character encoding such as UTF-8. Decoding works when both sides agree on the representation. A hash has a different job: it produces a fingerprint for comparison, not a message you can decode back.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
message = "Hi"
payload = message.encode("utf-8")
print(payload.hex())
print(bytes.fromhex("4869").decode("utf-8"))
Run and change this example ↗
Reveal the expected output
4869
Hi

Read it one step at a time

  1. encode changes the Python string into bytes using UTF-8.
  2. hex displays those bytes as hexadecimal digits. Here H corresponds to 48 and i to 69 in hex.
  3. fromhex reconstructs the bytes; decode turns them into text again. These steps need no secret key.

Make one change

Change the first message to "Hi!". What extra hexadecimal pair appears?

Check your thinking

21 appears at the end because it represents the exclamation mark byte.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.