Encoding information—turning plain text into another form—is a foundational idea in computer science. Whether you’re learning on CodeHS, building a classroom activity, or just curious, creating your own encoding is a fun way to practice logic, mapping, and debugging. This post walks through a simple, step-by-step approach to designing a custom encoding, explains common choices, and includes ready-to-run examples and classroom prompts.
To encode a string, you need to look at one character at a time, change it based on a rule, and add it to a new result string. 83 8 create your own encoding codehs answers
Common Encoding Rules:
Below is a functional Python solution that defines a custom encoding dictionary, allows the user to encode a message, and decode a binary string. Below is a functional Python solution that defines
# Part 1: Define the Encoding Scheme (The "Code Book")
# We map characters to unique binary strings.
# Note: A real scheme might use ASCII values, but here we create our own.
my_encoding =
'A': '00001',
'B': '00010',
'C': '00011',
'D': '00100',
'E': '00101',
'F': '00110',
'G': '00111',
'H': '01000',
'I': '01001',
'J': '01010',
'K': '01011',
'L': '01100',
'M': '01101',
'N': '01110',
'O': '01111',
'P': '10000',
'Q': '10001',
'R': '10010',
'S': '10011',
'T': '10100',
'U': '10101',
'V': '10110',
'W': '10111',
'X': '11000',
'Y': '11001',
'Z': '11010',
' ': '11111' # Encoding for Space
var encodingMap = 'a': '🐼', 'b': '🐻', 'c':
var encodingMap =
'a': '🐼', 'b': '🐻', 'c': '🐱', 'd': '🐶', 'e': '🐰',
'f': '🦊', 'g': '🐸', 'h': '🐵', 'i': '🐧', 'j': '🐦',
'k': '🐌', 'l': '🐞', 'm': '🐝', 'n': '🐳', 'o': '🐬',
'p': '🦄', 'q': '🐉', 'r': '🌲', 's': '⭐', 't': '☀️',
'u': '🌙', 'v': '⚡', 'w': '❄️', 'x': '🔥', 'y': '💧',
'z': '🌈', ' ': ' '
;
| Error | Why It Happens | Fix |
|-------|----------------|------|
| encoded string is empty | Forgot to loop through message | Add for (var i = 0; i < message.length; i++) |
| Spaces disappear | Space not in encodingMap | Add ' ': ' ' to map |
| Decoding returns gibberish | Decoding map built incorrectly | Ensure decodingMap[value] = key is correct |
| Infinite loop in decode | Not incrementing i properly | Always increment i by found length or 1 |
| Uppercase letters fail | Map only has lowercase | Use .toLowerCase() on each char |
def encode_message(message):
binary_output = ""
# Convert message to uppercase to match dictionary keys
message = message.upper()
for char in message:
if char in my_encoding:
binary_output += my_encoding[char]
else:
# Handle characters not in our dictionary (optional)
binary_output += "?????"
return binary_output