AQA 7517, section 4.5.5.2
A computer can only store numbers, so each character (a letter, digit, symbol or space) is given a number, called its character code. A character set is the list of characters and their codes. Both sides must use the same character set, or the text will be wrong.
ASCII
ASCII (American Standard Code for Information Interchange) is a character set that uses 7 bits, giving characters (codes 0 to 127). It includes:
- the capital letters
AtoZ(codes 65 to 90); - the lower case letters
atoz(codes 97 to 122); - the digits
0to9(codes 48 to 57); - punctuation and symbols;
- control characters such as newline and tab (codes 0 to 31).
| Character | A |
B |
a |
0 |
space |
|---|---|---|---|---|---|
| Code | 65 | 66 | 97 | 48 | 32 |
| Binary (8 bits) | 01000001 | 01000010 | 01100001 | 00110000 | 00100000 |
Useful patterns
- The codes for the letters are in order, so
'B'is one more than'A', which lets you sort text alphabetically by comparing codes. - A lower case letter's code is 32 more than the matching capital:
'a'(97) 32 ='A'(65).
print(ord("A")) # 65
print(chr(66)) # B
print(chr(ord("a") - 32)) # A
Extended ASCII
ASCII uses 7 bits, but a byte has 8, so the extended ASCII versions use the spare bit to give 256 characters (codes 0 to 255). The extra 128 codes hold accented letters and symbols, but different versions used them differently, which caused confusion.
The limits of ASCII
ASCII was designed for English. It has no room for:
- accented letters such as é, ü and ñ (partially in extended versions);
- other alphabets, such as Greek, Cyrillic, Arabic, Hindi, Chinese, Japanese and Korean;
- many symbols and emojis.
With only 128 or 256 codes, there is no way to hold all the world's writing in one character set.
Unicode
Unicode was introduced to solve this. It is a character set designed to give every character in every language its own unique code, so text in any language can be stored, shared and displayed correctly.
- It has over 140 000 characters, covering most of the world's writing systems, symbols and emojis.
- The first 128 codes are the same as ASCII, so ASCII text is also valid Unicode.
- Codes are written like U+0041 (
A) and U+00E9 (é). - Characters can be stored using different encodings, such as UTF-8, which uses 1 to 4 bytes per character, and UTF-16 and UTF-32.
| ASCII | Unicode | |
|---|---|---|
| Bits | 7 (or 8 for extended) | Up to 32 (variable length in UTF-8) |
| Number of characters | 128 (256 for extended) | Over 140 000 |
| Languages | English, basically | Almost all of the world's languages |
| Storage | 1 byte per character | 1 to 4 bytes per character (UTF-8) |
| Emoji | No | Yes |
Why was Unicode introduced?
- ASCII could not represent the characters needed for languages other than English.
- Different countries had made incompatible extended character sets, so text could display wrongly (garbled) when shared.
- The growth of the internet and global communication made one universal standard necessary.
The disadvantage is that Unicode text can use more storage: characters from outside the ASCII range need more than one byte.
Worked example
Task: how would the word Hi be stored in ASCII, and how many bits does it need?
His code 72, which is .iis code 105, which is .
Using one byte per character, Hi takes 2 bytes = 16 bits: 01001000 01101001.
The same word in UTF-8 gives the same bytes, because these two characters are within the first 128 codes.
Exam tip: to answer "why was Unicode introduced", give the problem (ASCII has too few codes for the world's languages and symbols) and the solution (Unicode gives every character a unique code, so any language can be stored and shared).
Check yourself
How many different characters can 7-bit ASCII represent?
.
The ASCII code for 'C' is 67. What is the code for 'F'?
70, because the letters are in order and F is three letters after C.
Why was Unicode introduced?
ASCII can't represent the characters of most of the world's languages and symbols. Unicode gives every character a unique code, so text in any language can be stored and shared.
What is one disadvantage of Unicode compared with ASCII?
Text can use more storage space, because characters outside the ASCII range need more than one byte.