Edexcel International Advanced Level Computer Science, point 2.3.1
Definition
A character encoding system is an agreed set of rules that gives every character a unique number (a code), so that text can be stored and sent as binary. A character set is the collection of characters the system covers.
Function and why needed
A computer can only store binary numbers. For text to be stored, displayed, sent and understood by different computers and programs, they must agree which number means which character. Without a shared standard, a message typed on one machine would appear as nonsense on another.
a. ASCII and extended ASCII
ASCII
- ASCII stands for American Standard Code for Information Interchange.
- It uses 7 bits per character, so it has codes (0 to 127).
- It includes:
- Control characters (codes 0 to 31 and 127), such as new line, tab and delete.
- The space (32).
- Digits
0to9(48 to 57). - Upper-case letters
AtoZ(65 to 90). - Lower-case letters
atoz(97 to 122). - Punctuation and symbols.
| Character | Denary | Binary (8-bit) | Hex |
|---|---|---|---|
0 |
48 | 00110000 | 30 |
A |
65 | 01000001 | 41 |
B |
66 | 01000010 | 42 |
a |
97 | 01100001 | 61 |
| space | 32 | 00100000 | 20 |
Useful patterns:
- The codes of the letters are in alphabetical order, so sorting codes sorts the letters.
- A lower-case letter's code is 32 more than the upper-case one (
ais 97 andAis 65). Only one bit (bit 5) differs, so the masks from 2.2.2 change the case: OR with00100000makes a letter lower case, and AND with11011111makes it upper case. - The digit
5has code , which is not the number 5. - You don't need to memorise the codes, but you should be able to work one out from another: if
Ais 65, thenEis 69.
Extended ASCII
- Extended ASCII uses 8 bits (one byte), giving codes (0 to 255).
- The first 128 codes are the same as ASCII. The extra 128 (128 to 255) hold accented letters, symbols and drawing characters, for example
é,ñand£. - There isn't one extended ASCII: different versions (code pages) use the extra codes differently, for different languages.
| ASCII | Extended ASCII | |
|---|---|---|
| Bits | 7 | 8 |
| Characters | 128 | 256 |
| Languages | English only | Several European languages (one at a time) |
Benefits and drawbacks of ASCII
- Benefits:
- Simple, and small: one byte per character.
- Widely supported, and fast to process, since every character is the same size.
- Letters and digits are in order, which makes sorting and comparing easy.
- Backward compatible: its 128 codes are the first 128 in Unicode and UTF-8, so ASCII text works in newer systems.
- Still used where only English text is needed, such as many embedded devices and network protocols, because it needs very little memory and processing.
- Drawbacks:
- Too few characters: it can't represent most of the world's languages, such as Chinese, Arabic and Hindi, and has no emoji.
- Extended ASCII's code pages conflict: text written with one can look wrong with another.
- Only one language at a time can be used in a file.
b. Unicode and UTF-8
Unicode
- Unicode is a universal character set that gives every character in (almost) every writing system a unique number, called a code point. It has over 150 000 characters, from scripts, symbols and emoji.
- Code points are written as U+ followed by hexadecimal, for example U+0041 is
A, U+00E9 isé, U+20AC is€, and U+1F600 is the grinning-face emoji. - The first 128 code points are the same as ASCII.
- Unicode defines the numbers. A separate encoding says how those numbers are stored as bytes. The common ones are UTF-8, UTF-16 and UTF-32.
UTF-8
- UTF-8 is a variable-length encoding: it uses 1 to 4 bytes for each character.
- ASCII characters take just 1 byte, and have exactly the same codes as ASCII, so plain ASCII text is already valid UTF-8.
- Other characters take 2, 3 or 4 bytes: for example,
éuses 2 bytes,€uses 3, and most emoji use 4.
Other Unicode encodings
- UTF-16: 2 or 4 bytes per character.
- UTF-32: always 4 bytes, which is simple but uses a lot of space.
Benefits of Unicode and UTF-8
- One standard for all languages, so text from anywhere can be mixed in one document and exchanged without errors.
- Includes emoji and symbols, and new characters can be added.
- UTF-8 is compatible with ASCII, and is compact for English text.
- UTF-8 is the most widely used encoding on the web.
Drawbacks
- Larger files than ASCII for text that isn't English (2 to 4 bytes per character).
- Variable length makes it harder to process: you can't jump to the th character by calculating a position, and counting characters means reading the bytes.
- More complicated to implement, and needs more processing and fonts that cover the characters.
- Wrongly mixing encodings produces garbled text. For example,
éin UTF-8 is the two bytesC3 A9; software that reads one byte per character (extended ASCII) shows each byte as its own character, givingéinstead ofé.
Comparison
| ASCII | Extended ASCII | UTF-8 | |
|---|---|---|---|
| Bytes per character | 1 (7 bits used) | 1 | 1 to 4 |
| Number of characters | 128 | 256 | Over 1 million possible (150 000+ assigned) |
| Languages | English | A few European | All |
| File size for English text | Smallest | Same | Same as ASCII |
Worked example
Task: a text contains Café 😀. Work out its size in bytes in (a) UTF-8, and (b) UTF-32, and say which suits English documents better.
(a) UTF-8:
| Character | C |
a |
f |
é |
space | emoji |
|---|---|---|---|---|---|---|
| Bytes | 1 | 1 | 1 | 2 | 1 | 4 |
Total bytes.
(b) UTF-32: 6 characters 4 bytes bytes.
UTF-8 is less than half the size here, and for an English document, which is almost all ASCII characters, it would be about a quarter the size of UTF-32. So UTF-8 suits English text better. For a document in a script that needs 3 bytes per character in UTF-8, UTF-16 (2 bytes) could be smaller.
Exam tip: give the number of bits and the number of characters for ASCII (7 bits, 128) and extended ASCII (8 bits, 256). For characteristics of UTF-8, three points earn marks: it can represent many writing systems, symbols and emoji; it is variable width, 1 to 4 bytes per character; and it is backward compatible with ASCII. A good comparison point is file size against the range of characters. Be careful with character codes: '5' is code 53, not 5.
Check yourself
How many different characters can 7-bit ASCII represent?
128 (2⁷).
The code for A is 65. What is the code for D?
Give two limitations of ASCII
Any two of: only 128 characters; can't represent most languages; no emoji; extended ASCII versions conflict with each other.
Why is UTF-8 described as backward compatible with ASCII?
ASCII characters use the same single-byte codes in UTF-8, so any ASCII text is already valid UTF-8.
Give one advantage and one disadvantage of UTF-8 compared with ASCII
Advantage: it can represent every language, plus symbols and emoji. Disadvantage: characters vary in length (1 to 4 bytes), and non-English text takes more space and is harder to process.