Character sets and encoding
Character Sets and Encoding
Every piece of text stored or processed by a computer must be represented internally as binary numbers. A character set is a defined collection of characters that a computer system recognises and can represent - this includes letters, digits, punctuation marks, spaces, and special control characters. A character encoding is the system used to assign a unique numerical value (called a character code or code point) to each character in that set. When you press a key on a keyboard, the computer stores the corresponding character code in binary form.
7-bit ASCII
ASCII (American Standard Code for Information Interchange) was one of the earliest widely adopted character encoding standards. The original ASCII standard uses 7 bits per character, which means it can represent 27 = 128 unique characters, with codes ranging from 0 to 127.
The 128 ASCII characters include uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), common punctuation and symbols, and a set of control characters - such as newline, tab, and carriage return - used to format text or control output devices. The characters are arranged in a deliberate order: for example, all uppercase letters occupy a consecutive range, as do all lowercase letters.
ASCII Character Code Table (Selected Values)
| Character | Decimal Code | Binary (7-bit) | Hexadecimal |
|---|---|---|---|
| Space | 32 | 0100000 | 0x20 |
| A | 65 | 1000001 | 0x41 |
| B | 66 | 1000010 | 0x42 |
| C | 67 | 1000011 | 0x43 |
| a | 97 | 1100001 | 0x61 |
| b | 98 | 1100010 | 0x62 |
| 0 (digit) | 48 | 0110000 | 0x30 |
| 1 (digit) | 49 | 0110001 | 0x31 |
A useful pattern to notice: the ASCII codes for the uppercase and lowercase versions of the same letter differ by exactly 32. For example, the code for "A" is 65 and the code for "a" is 97 (97 - 65 = 32). This is a deliberate design decision that makes switching between cases straightforward to compute.
Unicode
Unicode is an international standard that assigns a unique code point to every character in every written language, as well as technical symbols and emoji. Unicode currently defines over 140,000 characters across more than 150 writing systems.
Unicode code points are written in the form U+XXXX, where XXXX is a hexadecimal number. For example, the capital letter "A" is U+0041, the Euro sign is U+20AC, and a snowman symbol is U+2603. Because Unicode uses more bits per character than ASCII, Unicode-encoded files are generally larger - but the benefit of representing all world scripts in a single document far outweighs this cost.
Comparing ASCII and Unicode
| Feature | 7-bit ASCII | Unicode |
|---|---|---|
| Number of characters | 128 | Over 140,000 (and growing) |
| Bits per character | 7 bits (often stored in 8) | More bits - supports a far larger code point space |
| Languages supported | English only (basic Latin) | All major world scripts |
| File size (compared) | Smaller per character | Larger per character |
| Backwards compatibility | N/A | First 128 code points are identical to ASCII |
An important design feature of Unicode is that its first 128 code points (U+0000 to U+007F) are identical to the ASCII character set. This means that any text encoded in ASCII is automatically valid Unicode, making it practical to migrate older systems to the newer standard.
Deep Dive: Converting Between Characters and Codes
AQA requires you to be able to use a character encoding table to convert in both directions: from a character to its code, and from a code back to its character. The examples below use a subset of the standard ASCII table. In the exam, a table will always be provided - you do not need to memorise character codes.
Converting a Character to Its Code
To encode a character, locate it in the encoding table and read off its corresponding decimal value. Then convert that decimal value to binary if a binary representation is required.
Example: Encode the word CAB using 7-bit ASCII.
- Find "C" in the ASCII table: decimal 67, binary
1000011 - Find "A" in the ASCII table: decimal 65, binary
1000001 - Find "B" in the ASCII table: decimal 66, binary
1000010
The word "CAB" is stored as: 1000011 1000001 1000010
0100000.
Converting a Code to Its Character
To decode a binary code, first convert the binary value to decimal. Then use the encoding table to find the character that matches that decimal code.
Example: Decode the following 7-bit ASCII codes: 1000010 1000001 1000111
- Convert
1000010to decimal: 64 + 2 = 66 - look up 66 in the table - "B" - Convert
1000001to decimal: 64 + 1 = 65 - look up 65 in the table - "A" - Convert
1000111to decimal: 64 + 4 + 2 + 1 = 71 - look up 71 in the table - "G"
The decoded message is: BAG
Key Takeaways
- A character set is a defined collection of characters a computer can represent; each character is assigned a unique numerical character code.
- 7-bit ASCII uses 7 bits per character, supporting exactly 128 characters (codes 0 to 127), covering basic English letters, digits, punctuation, and control characters.
- Unicode is an international standard that supports over 140,000 characters from all major world scripts, with code points written in the form U+XXXX.
- The first 128 Unicode code points (U+
0000to U+007F) are identical to ASCII, ensuring backwards compatibility. - Unicode requires more storage per character than ASCII because it uses more bits to support its far larger character set.
- To convert using an encoding table: find the character to get its code, or find the code (converting from binary to decimal first) to get its character.