Text as Numbers with ASCII and Unicode
See how characters become numbers with ASCII and Unicode, find patterns in character codes, and learn how UTF-8 stores text in one to four bytes.
Welcome back!
Step 1 of 12Learn
A number for every letter
When you type the letter A, the computer doesn't store a picture of an A. It stores a number, and every program agrees that this number means A. The agreement is called a character encoding.
The classic encoding is ASCII (American Standard Code for Information Interchange), created in the 1960s. It uses 7 bits, giving 27 = 128 codes, enough for:
- uppercase letters A to Z (codes 65 to 90)
- lowercase letters a to z (codes 97 to 122)
- digits 0 to 9 (codes 48 to 57)
- punctuation and symbols, the space (code 32), and invisible control codes such as a new line (code 10)
So the word Hi is stored as 72, 105, or in binary 01001000 01101001.
Notice that the digit "7" is not the number 7. As a character, it's code 55. When a program reads "7" from a text box, it must convert the character to the number before doing math, which is why mixing up text and numbers causes bugs like "7" + "1" becoming "71."
ASCII was designed for English. It has no letters with accents, no Greek, Arabic, Chinese or Hindi characters and certainly no emoji. That limitation led to a much bigger standard.
Step 2 of 12Diagram
What happens when you type A
Tap a shape with to see more.
- Press A
- Code 65
The agreed number for capital A.
- 01000001
Stored or sent as one byte.
- Font draws A
The receiving program looks up 65 and shows the letter shape.
Step 3 of 12Practice · Work it out
In ASCII, the capital letter A is 65 and the letters follow in alphabetical order. What is the code for the capital letter E?
Step 4 of 12Learn
Clever patterns in the codes
ASCII's designers arranged the codes so that common tasks are easy in binary.
Uppercase and lowercase differ by 32. A is 65 (01000001) and a is 97 (01100001). The only difference is one bit, the one worth 32. Changing case is just flipping that bit. That's why converting text to uppercase is so fast.
Digits line up with their values. The character "0" is 48, "1" is 49 and so on up to "9" at 57. Subtract 48 from a digit's code and you get its numeric value: "7" is 55, and 55 − 48 = 7. In binary, 48 is 00110000, so the last four bits of each digit character are the digit itself in binary.
Alphabetical order is numeric order. Sorting words means comparing codes. But since all uppercase codes are smaller than all lowercase codes, a naive sort puts "Zebra" before "apple." Programs that sort "naturally" first convert everything to the same case.
These little patterns show a recurring theme in computing: a well-chosen representation makes the operations you need simple and fast.
Step 5 of 12Practice · Match the pairs
Match each character to its ASCII code.
Tap an answer, then tap the place where it belongs (or drag it there).
- B
- z
- 0
- (space)
- 5
Step 6 of 12Practice · Diagram
A map of the ASCII codes
Tap the code for the digit character "0".
Tap the shape that answers the question, then press Check.
- Space 32
32
- "0" 48
48
- "A" 65
65
- "a" 97
97
- DEL 127
127
Step 7 of 12Learn
Unicode and UTF-8
As computers spread worldwide, every region invented its own encoding, and text sent between them often turned into garbage symbols. Unicode solved this with one universal list. It assigns a unique number, called a code point, to every character in essentially every writing system, plus symbols and emoji. The standard contains well over 150,000 characters and keeps growing.
Code points are usually written in hex with a U+ prefix. The letter A is U+0041 (hex 41 = 65, the same as ASCII), the euro sign € is U+20AC and the grinning face emoji is U+1F600.
A code point is just a number; it still has to be stored in bytes. The most common way is UTF-8, used by the overwhelming majority of web pages. UTF-8 uses a variable number of bytes:
| Characters | Bytes in UTF-8 |
|---|---|
| Basic Latin (English letters, digits, ASCII symbols) | 1 |
| Accented Latin, Greek, Cyrillic, Hebrew, Arabic and more | 2 |
| Most Chinese, Japanese and Korean characters | 3 |
| Emoji and rarer characters | 4 |
A brilliant design choice: the first 128 Unicode characters are encoded in UTF-8 exactly as in ASCII, so every old ASCII file is also valid UTF-8. That backward compatibility is a big reason UTF-8 took over the web.
Step 8 of 12Practice · Chart
Bytes per character in UTF-8
Tap the character that needs exactly 3 bytes.
Tap the bar that answers the question, then press Check.
Step 9 of 12Note
Step 10 of 12Practice · Work it out
A message contains the word "Hello" followed by one emoji. Using UTF-8, how many bytes does it take?
Step 11 of 12Practice · Code challenge
Lowercase letters are exactly 32 higher than uppercase ones in ASCII. Write toUpper(word) that converts a word made only of lowercase letters a to z into uppercase, using charCodeAt and String.fromCharCode, not toUpperCase. For example, toUpper("hi") returns "HI".
toUpper("hi")toUpper("binary")toUpper("z")toUpper("")
Step 12 of 12Practice · True or false
In ASCII, the character "8" is stored as the number 8.
Lesson complete!