Modules / Encoding / ASCII

ASCII Code Explorer

Visualize how text characters are represented as numerical codes in computers.

Overview

What is ASCII?

ASCII, which stands for American Standard Code for Information Interchange, is a character encoding standard. In simple terms, it's a universal dictionary that assigns a unique number to every letter, digit, and symbol you can type. Computers don't understand letters like 'A' or 'b'; they only understand numbers. ASCII translates our human-readable characters into a numerical format.

Character Representations

0 CHARS

Enter text to see ASCII codes

Tip: ASCII encodes 128 specified characters into 7-bit integers.

Understanding ASCII: The Language of Computers

Explore how every character you type is converted into a universal numerical code that computers can understand.

How It Works: From Character to Code

The standard ASCII table contains 128 unique codes, numbered from 0 to 127. Each code represents a specific character. For example:

  • The character 'A' is assigned the ASCII code 65.
  • The character 'a' is assigned the ASCII code 97.
  • The digit '5' is assigned the ASCII code 53.
  • A space character ' ' is assigned the ASCII code 32.

When you type "Hello", the computer sees the sequence of numbers: 72, 101, 108, 108, 111.

Characters as numbers

Computers store numbers. ASCII was the agreement, from 1963, about which number means which character — 128 codes covering the English alphabet, digits, punctuation and control characters.

CharacterDecimalHex
NUL000
\n (newline)100A
space3220
04830
A6541
Z905A
a9761
z1227A

Three of those ranges are worth memorising because they make arithmetic tricks possible.

Digits start at 48, so ord('7') - ord('0') gives 7 — converting a character to its numeric value without a lookup table.

Uppercase starts at 65 and lowercase at 97, exactly 32 apart. So chr(ord('A') + 32) is 'a', and since 32 is a single bit (2⁵), case conversion is a bit flip — which is why it was historically so cheap.

Letters are contiguous, so 'a' <= c <= 'z' tests for a lowercase letter, and sorting strings by code point sorts alphabetically within a case.

ord('A')            # 65
chr(97)             # 'a'
ord('a') - ord('A') # 32
'A' < 'a'           # True - uppercase sorts first

That last line is a real source of surprise: sorting a list of names puts every capitalised entry before every lowercase one, because 'Z' (90) is less than 'a' (97).

Why 128 was not enough

ASCII covers English. It has no é, no ñ, no ö, no Cyrillic, no Greek, no Arabic, no Chinese, no emoji.

The first response was a proliferation of 8-bit extensions — Latin-1 for Western European, Windows-1252, KOI8-R for Russian, and dozens more — each redefining codes 128–255 differently. A file was only readable if you knew which encoding it used, and the classic symptom of guessing wrong is text like "é" where "é" should be.

Unicode is the modern answer: one code point per character, covering every script, currently over 149,000 assigned. é is U+00E9, is U+4E2D, and 🎉 is U+1F389.

Unicode assigns numbers; it does not say how to store them. That is what an encoding does, and UTF-8 is the one that won:

CharacterCode pointUTF-8 bytes
AU+00411 byte: 41
éU+00E92 bytes: C3 A9
U+4E2D3 bytes: E4 B8 AD
🎉U+1F3894 bytes: F0 9F 8E 89

UTF-8's decisive property is that the first 128 code points encode as single bytes identical to ASCII. Every ASCII file is already valid UTF-8, which is why adoption was possible at all.

Where this matters in NLP

Characters are not bytes. len("café") is 4 characters and 5 UTF-8 bytes. Truncating a string at a byte boundary can split a character in half and produce invalid data — a real bug when enforcing length limits on user input.

The same text can have two encodings. "é" may be one code point (U+00E9) or two (e + combining accent U+0301). They look identical and compare as different strings, which is the most common cause of "these records should match and do not". unicodedata.normalize("NFKC", text) resolves it, and should be the first step of any text pipeline.

Tokenisers work on bytes or characters. Byte-level BPE, used by GPT models, operates on UTF-8 bytes, so it can represent any text at all — at the cost of more tokens for non-Latin scripts, since each character consumes several bytes.

Encoding errors are silent or fatal. Reading a UTF-8 file as Latin-1 succeeds and produces mojibake; reading a Latin-1 file as UTF-8 raises UnicodeDecodeError. Always specify the encoding explicitly.

Before any of it is text, it is numbers

Every string is bytes, and the mapping from bytes to characters is where a surprising amount of NLP goes wrong. ASCII, Unicode and UTF-8 are all inspected here directly.

example_01.pyNumPy
Output

Try it yourself

Use the "ASCII Code Explorer" above to see this process in action:

  1. Type Your Name: In the "Input Text" box, type your name. Notice how each character, including the space, instantly appears in the "Character Representations" area with its corresponding Decimal, Hexadecimal, and Binary codes.
  2. Case Sensitivity: Type the letter 'P' and then 'p'. Observe their ASCII codes (80 and 112). This difference is why passwords are often case-sensitive. Computers see 'P' and 'p' as completely different characters.
  3. Numbers vs. Digits: Type the number '9'. Its ASCII code is 57. This is different from the numerical value 9. The character '9' is text, just like 'A' or '$'.
  4. Special Characters: Try typing symbols like '@', '#', or '!'. Each has its own unique code, allowing for complex text and programming syntax.
  5. Check the Statistics: The stats panel shows you the total characters and bytes. In standard ASCII, one character takes up one byte (8 bits) of memory.

Why is ASCII Important in NLP?

ASCII is a foundational concept in Natural Language Processing (NLP). While modern systems often use more advanced encodings like UTF-8 to support emojis and multiple languages, the core principle remains the same: text must be converted to numbers for a machine to process it. Understanding ASCII helps you grasp fundamental NLP tasks:

  • Text Normalization: Converting all text to lowercase (e.g., changing 'A' (65) to 'a' (97)) is a common preprocessing step to ensure words like "The" and "the" are treated as the same.
  • Feature Engineering: The numerical representation of characters can be used as features for machine learning models to identify patterns.
  • Data Cleaning: Identifying and removing non-ASCII characters can be crucial when working with datasets that should only contain standard English text.

In one line

ASCII is the essential bridge between human language and computer processing. It standardizes the representation of text as numbers, a fundamental step for all digital communication and computation. By interacting with the explorer, you can build a solid intuition for this cornerstone of computer science and NLP.

Practical handling

# always specify the encoding
with open("data.txt", encoding="utf-8") as f:
    text = f.read()

# be explicit about failures
text = raw_bytes.decode("utf-8", errors="replace")   # bad bytes become U+FFFD
text = raw_bytes.decode("utf-8", errors="ignore")    # bad bytes dropped

# normalise before comparing
import unicodedata
a = unicodedata.normalize("NFKC", a)
b = unicodedata.normalize("NFKC", b)
a == b

Python 3 keeps this straight by separating the two types: str is a sequence of Unicode code points, bytes is a sequence of 8-bit values. You cannot concatenate them, which prevents a whole class of bug that Python 2 permitted.

The four normalisation forms: NFC composes characters where possible, NFD decomposes them, and NFKC/NFKD additionally fold compatibility variants (full-width characters, ligatures, superscripts). NFKC is the usual choice for text processing, because it collapses the largest number of visually-equivalent forms.

Other things worth knowing

Control characters occupy codes 0–31 and 127. Most are historical — bell, form feed, device control — and their presence in scraped text usually indicates a problem. Strip them.

Line endings differ. Unix uses \n (10), Windows uses \r\n (13, 10). Reading in text mode normalises them; reading in binary mode does not, and stray \r characters at line ends are a common annoyance.

Zero-width and invisible characters exist and cause trouble: zero-width space (U+200B), zero-width joiner, byte-order mark (U+FEFF). A BOM at the start of a file appears as an invisible character in the first field, which breaks header parsing in CSV files — encoding="utf-8-sig" strips it.

Emoji are often several code points. A single visible emoji may combine a base character, a skin-tone modifier and a zero-width joiner, so len() on an emoji string rarely matches what the user sees.

Questions people ask

Is ASCII still relevant? As a subset of UTF-8, yes — and never as a complete encoding for real-world text.

Why is 'A' < 'a'? Because 65 < 97. Sorting by code point puts all uppercase before all lowercase.

What is mojibake? Text decoded with the wrong encoding — "café" appearing as "café". The fix is to identify the true encoding, not to patch the symptoms.

Should I always use UTF-8? Yes, for output. For input, detect or be told; chardet guesses when nothing else is available.

Why does my string length look wrong? Characters, bytes and user-perceived characters are three different counts. Emoji and combining accents make them diverge.

What is a byte-order mark? An invisible marker at the start of a file indicating the encoding. Harmless in principle, and it corrupts the first field of a CSV; use utf-8-sig to strip it.

Recap in one screen

  • ASCII maps 128 characters to numbers; digits at 48, uppercase at 65, lowercase at 97.
  • The 32-place gap between cases makes case conversion a single bit flip.
  • Unicode assigns code points for every script; UTF-8 stores them, with ASCII as a compatible subset.
  • Characters, code points and bytes are three different counts — truncate on the right one.
  • Normalise with NFKC before comparing anything, and always state the encoding when reading files.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. Without scrolling back — what is the one-line takeaway from this module?

  2. What does this module say about “What is ASCII”?

  3. What does this module say about “How It Works: From Character to Code”?

Cheat sheet

ASCII Character Codes

ASCII, which stands for American Standard Code for Information Interchange, is a character encoding standard. In simple terms, it's a universal dictionary that assigns a unique number to every letter, digit, and symbol you can type. Computers don't understand letters like 'A' or 'b'; they only understand numbers. ASCII translates our human-readable characters into a numerical format.

NLP · vizlearn.in/natural_language_processing/ascii_codes.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.