Lesson 59: Character codes
Letters are numbers too
- A computer stores only binary. Each character has a number in a character set, and the number is stored in binary.
- ASCII uses 7 or 8 bits per character: up to 256 characters, enough for English.
- Unicode uses up to 32 bits per character: many more characters, for many languages and emojis.
Words for this lesson
| Word | Meaning |
|---|---|
| character set | a list of characters, each with its own number |
| ASCII | a small character set (128 or 256 characters) |
| Unicode | a big character set for almost every language |
Some ASCII codes
| Characters | Codes |
|---|---|
| 0 to 9 | 48 to 57 |
| A to Z | 65 to 90 |
| a to z | 97 to 122 |
- Capital and small letters are always 32 apart.
In the exam
- 0478/12/F/M/24 Q1(b): N is 78 in ASCII. (i) The code for Q: 81 (three letters later). (ii) N in binary: 01001110. (iii) How RED is stored with a character set. Mark scheme: each character has a unique binary number, and the codes for R, E and D are stored in sequence.
- 0478/11/O/N/23 Q5(d): differences between ASCII and Unicode. Mark scheme: ASCII has fewer characters (128/256) and fewer languages and uses 7 or 8 bits per character; Unicode has many more characters, including many languages and emojis, and uses up to 16 or 32 bits per character.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Every character is stored as a number from a character set: in ASCII, R is 82. Type a word. For each character, print one line: the character, =, its code and = its 8 bits, for example R = 82 = 01010010. (int)c gives the code; printBits(c) prints the bits.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Small letters come 32 places after capital letters: a is 97 and A is 65. Write your own upper-case converter: if a character is from 'a' đến 'z', take 32 away from it. Other characters stay the same. Type Hello, world! and the program prints HELLO, WORLD!.
Nhấn Chạy để xem kết quả ở đây.