Algorithmsascii character mapping

Understanding ASCII Character Mapping

TT
Testlaa Team
May 14, 20261 min read

ASCII maps 128 characters to integers 0..127. ord() turns a character into its code; chr() turns a code back into a one-character string. This bidirectional map is the foundation of indexing, hashing, and bit tricks on text.

Why this shows up in the real world

Telemetry packets often carry single-byte command codes. Serial devices map bytes to printable glyphs for debug logs. Cryptography homework XORs ASCII bytes with keys. All of it assumes you understand the numeric spine behind each glyph.

Core idea (explained for students)

Examples: ord('A') == 65, chr(65) == 'A'. Lowercase letters are contiguous (ord('a') to ord('z')). Digits '0'..'9' occupy 48..57. Knowing these blocks lets you convert '7' to integer 7 via ord(ch) - ord('0') without int(ch) if you want to show the pattern.

Try this in Python

def ascii_codes(s: str) -> list[int]:
    return [ord(c) for c in s if c.isascii()]


print(ascii_codes("Hi99"))

Common mistakes

  • Calling chr on numbers outside 0..0x10FFFF (Unicode limit)—chr accepts up to that, but ASCII problems stay in 0..127.
  • Mixing bytes (b'A') with str in Python 3—decode or compare consistently.

Key takeaways

  • Memorize three blocks: digits, uppercase, lowercase offsets.
  • Use ord/chr to explain why Caesar ciphers and cyclic shifts work.

Tags:

StringsPythonStudents