OCR H446, section 1.4.1 (j)
A computer can only store numbers, so each character (a letter, digit, symbol or space) is given a number, its character code. A character set is the list of characters and their codes. For text to be shown correctly, both sides must use the same character set.
ASCII
ASCII (American Standard Code for Information Interchange) uses 7 bits, giving characters, with codes 0 to 127. It is usually stored in one byte per character.
| Range | Characters |
|---|---|
| 0 to 31 | Control characters (such as newline and tab) |
| 32 | Space |
| 48 to 57 | The digits 0 to 9 |
| 65 to 90 | Capital letters A to Z |
| 97 to 122 | Lower case letters a to z |
| Others | Punctuation and symbols |
| Character | 0 |
A |
B |
a |
space |
|---|---|---|---|---|---|
| Code | 48 | 65 | 66 | 97 | 32 |
| Binary | 00110000 | 01000001 | 01000010 | 01100001 | 00100000 |
Patterns in the codes
- The letters are in order, so
Bis one more thanA. This means that text can be sorted alphabetically by comparing codes. - A lower case letter's code is 32 more than the capital:
a(97) minus 32 isA(65). - The digit
7has code 55 (), soord(c) - 48gives a digit's value.
Extended ASCII
Since a byte has 8 bits, extended ASCII uses the extra bit to give 256 characters (codes 0 to 255). The extra 128 codes hold accented letters and symbols. Different versions used them differently, which caused confusion.
Limitations of ASCII
It was designed for English. It has no room for:
- the alphabets of other languages (Greek, Cyrillic, Arabic, Chinese, Japanese, Hindi);
- many accented letters and symbols;
- emoji.
Unicode
Unicode is a character set that gives every character in every writing system its own unique code, so that text in any language can be stored, shared and displayed.
- It has over 140 000 characters, including emoji and historic scripts.
- The first 128 codes are the same as ASCII, so ASCII text is valid Unicode.
- Codes are written like U+0041 (
A) and U+00E9 (é). - Different encodings store the codes differently:
| Encoding | Bytes per character | Notes |
|---|---|---|
| UTF-8 | 1 to 4 (variable) | ASCII characters take 1 byte. The most common on the web. Compact for English |
| UTF-16 | 2 or 4 | Used inside some systems |
| UTF-32 | 4 (fixed) | Simple but uses a lot of space |
ASCII compared with Unicode
| ASCII | Unicode | |
|---|---|---|
| Bits per character | 7 (usually stored in 8) | Up to 32 (variable in UTF-8) |
| Number of characters | 128 (256 extended) | Over 140 000 |
| Languages | English, mainly | Almost all of the world's |
| Emoji and symbols | No | Yes |
| Storage of English text | 1 byte per character | 1 byte in UTF-8 |
| Storage of other text | Not possible | 2 to 4 bytes per character |
Why was Unicode needed?
- ASCII couldn't represent most of the world's languages.
- Different countries made incompatible extended sets, so text could appear garbled when shared.
- The Internet and global communication needed one universal standard.
Disadvantage of Unicode: text can need more storage, since characters outside ASCII use more than one byte.
Calculating the size of text
Example: a message of 500 characters in ASCII, using 8 bits for each: bits bytes.
Example: 1000 characters in UTF-32: bits bytes. In UTF-8, if they are all ASCII, only bytes.
Worked example
Task: (a) how is the word Hi! stored in ASCII? (b) How many different characters can 7-bit ASCII represent, and why is that a problem? (c) How would a program decide that "apple" comes before "banana"?
- (a)
H= 72 = ,i= 105 = ,!= 33 = . Three bytes, bits:01001000 01101001 00100001. - (b) . That isn't enough for the characters of languages other than English, so Unicode was developed.
- (c) It compares the codes, character by character:
a(97) is less thanb(98), soapplecomes first.
Exam tip: to explain why Unicode was introduced, give the problem (ASCII has too few codes for the world's languages and symbols) and the solution (a unique code for every character, so any text can be stored and shared). Link character codes to the order of the letters for sorting.
Check yourself
How many characters can 7-bit ASCII represent?
.
The ASCII code for C is 67. What is the code for F?
70, since the letters are in order and F is three 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 that text in any language can be stored and shared.
How many bits does 200 characters of ASCII text need, at 8 bits each?
bits (200 bytes).