Characters and character sets
Characters in binary
Every character you type, whether a letter, a digit, a punctuation mark, a space or even a new line, is stored as a binary code. A character set is the list of characters a computer can use, with the code for each one.
Computers must use the same character set to share text. If they don't, the receiver turns the codes into the wrong characters, and you see nonsense.
The number of bits per character limits how many characters a set can have. With n bits there are 2ⁿ different codes:
| Bits per character | Number of characters |
|---|---|
| 7 | 2⁷ = 128 |
| 8 | 2⁸ = 256 |
| 16 | 2¹⁶ = 65,536 |
Key terms
ASCII
ASCII uses 7 bits per character, so it has 128 characters: the English upper- and lower-case letters, the digits 0–9, punctuation, the space, and control codes such as a new line. Extended ASCII uses 8 bits, giving 256 characters, adding accented letters such as é and symbols such as £.
The codes are in order: A is 65, B is 66, C is 67, and so on, and a is 97. So if you're told that A is 65, you can work out that E is 69.
In the exam, ASCII codes in binary use 8 bits: A is 01000001. You don't need to learn the codes, as the question gives you any you need.
Remember
Character codes are sequential: each letter's code is one more than the letter before.
Key terms
Unicode
ASCII only has room for English. Unicode is a much bigger character set, covering the characters of almost every language in the world, such as Arabic, Chinese and Hindi, plus maths symbols and emoji. It now has over 170,000 characters.
To give each one its own code, Unicode needs more bits per character: commonly 16 or 32 bits. Its first 128 codes are the same as ASCII, so A is still 65.
| ASCII | Unicode | |
|---|---|---|
| Bits per character | 7 (8 for extended) | 16 or 32 |
| Number of characters | 128 (256) | Over 170,000 |
| Languages | English | Almost all |
| File size | Smaller | Bigger, as each character takes more bits |
Key terms
Text file size
text file size = bits per character × number of characters
The answer is in bits; divide by 8 for bytes.
Example: a message of 500 characters is stored in 8-bit ASCII. How big is it?
- 8 × 500 = 4,000 bits
- 4,000 ÷ 8 = 500 bytes
The same message stored with 16 bits per character in Unicode takes 16 × 500 = 8,000 bits, or 1,000 bytes: twice the size.
Character codes in code
Exam Reference Language turns a character into its code with ASC and back with CHR. Python uses ord and chr:
code = ASC("B")
print(code)
print(CHR(code + 1))
Both print 66, then C, the next character in the set.