CS19: Character Encoding
How computers represent text using ASCII and Unicode character encoding, key codes to remember, and converting between characters and codes.
How computers represent text using ASCII and Unicode character encoding, key codes to remember, and converting between characters and codes.
Without character encoding, the binary pattern 01000001 could mean anything. Encoding standards ensure that 01000001 is always interpreted as the letter 'A'.
| Character | ASCII Code (Decimal) | Binary |
|---|---|---|
| 'A' | 65 | 01000001 |
| 'B' | 66 | 01000010 |
| 'Z' | 90 | 01011010 |
| 'a' | 97 | 01100001 |
| 'b' | 98 | 01100010 |
| 'z' | 122 | 01111010 |
| '0' | 48 | 00110000 |
| '1' | 49 | 00110001 |
| '9' | 57 | 00111001 |
| ' ' (space) | 32 | 00100000 |
What is the ASCII code for 'D'?
'A' = 65, 'B' = 66, 'C' = 67, 'D' = 68
What is the ASCII code for 'f'?
'a' = 97, 'b' = 98, 'c' = 99, 'd' = 100, 'e' = 101, 'f' = 102
code ← ASC('A') // code = 65
char ← CHR(65) // char = 'A'
// Convert uppercase to lowercase
upper ← 'G'
lower ← CHR(ASC(upper) + 32) // lower = 'g'
| Code Range | Characters | Count |
|---|---|---|
| 0-31 | Control codes (non-printable: newline, tab, bell) | 32 |
| 32-47 | Symbols and space | 16 |
| 48-57 | Digits 0-9 | 10 |
| 65-90 | Uppercase letters A-Z | 26 |
| 97-122 | Lowercase letters a-z | 26 |
| Character | Description | Unicode Code Point |
|---|---|---|
| e-acute | Latin small letter e with acute | U+00E9 |
| n-tilde | Latin small letter n with tilde | U+00F1 |
| Chinese "middle" | Chinese character | U+4E2D |
| Grinning face | Emoji | U+1F600 |
| Pound sign | Currency symbol | U+00A3 |
| Euro sign | Currency symbol | U+20AC |
| Feature | ASCII | Unicode |
|---|---|---|
| Number of characters | 128 | Over 1 million |
| Bits per character | 7 bits (stored in 1 byte) | Variable (8-32 bits depending on encoding) |
| Languages supported | English only | All world languages |
| Emojis supported | No | Yes |
| File size | Smaller (1 byte per character) | Larger (2+ bytes for non-ASCII chars) |
| Compatibility | Universal for English | Backwards compatible with ASCII |
| When to use | English-only text, small file size needed | Multi-language text, modern applications |
Unicode code points 0-127 are the same as ASCII:
This means any valid ASCII text is also valid Unicode text.
// Character to code
OUTPUT ASC('A') // 65
OUTPUT ASC('a') // 97
OUTPUT ASC('0') // 48
OUTPUT ASC(' ') // 32
// Code to character
OUTPUT CHR(65) // A
OUTPUT CHR(97) // a
OUTPUT CHR(48) // 0
INPUT "Enter a character: ", char
code ← ASC(char)
IF (code >= 65 AND code <= 90) OR (code >= 97 AND code <= 122) THEN
OUTPUT char, " is a letter"
ELSE
OUTPUT char, " is not a letter"
ENDIF
// The character '7' has ASCII code 55, not value 7
digitChar ← '7'
digitValue ← ASC(digitChar) - ASC('0') // 55 - 48 = 7
OUTPUT digitValue // 7 (the number, not the character)
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Thinking ASCII can store any character | ASCII only has 128 characters - no foreign languages or emojis | Use Unicode for non-English characters |
| Confusing '5' and 5 | The character '5' has code 53, not value 5 | Use ASC('5') - 48 to get the numeric value |
| Forgetting the +32 offset | 'a' is not code 65 + 1, it is 65 + 32 = 97 | Remember: lowercase = uppercase code + 32 |
| Saying Unicode replaces ASCII | Unicode includes ASCII (first 128 codes are the same) | Unicode extends ASCII - it is backwards compatible |
| Not considering storage size | Unicode can use more bytes per character | Be aware of file size implications |
Q1: What is the ASCII code for the letter 'M'?
Q2: How many characters can 7-bit ASCII represent?
Q3: Give two advantages of Unicode over ASCII.
Q4: Write pseudo-code that converts a lowercase letter to uppercase.
Q5: Explain why the character '3' and the integer 3 are stored differently in a computer.
INPUT letter
code ← ASC(letter)
IF code >= 97 AND code <= 122 THEN
upper ← CHR(code - 32)
OUTPUT upper
ELSE
OUTPUT "Already uppercase or not a letter"
ENDIF
✗ Thinking ASCII and Unicode store the same number of characters ✓ ASCII uses 7 bits (128 characters) or 8 bits (Extended ASCII, 256 characters). Unicode uses up to 32 bits and can represent over 143,000 characters from all world languages.
✗ Believing Unicode always uses 4 bytes per character ✓ Unicode uses variable-length encoding: UTF-8 uses 1-4 bytes per character. Common ASCII characters still use just 1 byte in UTF-8, making it backward-compatible.
✗ Forgetting that ASCII cannot represent non-English characters ✓ ASCII only covers English letters, digits, and basic symbols. It cannot represent characters from other languages (Chinese, Arabic, etc.) — this is why Unicode was developed.
✗ Confusing character code with the character itself ✓ A character code (e.g. 65 in ASCII) is the numeric representation stored in memory. The character 'A' is how it is displayed. The computer stores 65, not 'A'.
Explain why Unicode was developed as an alternative to ASCII, and discuss one advantage and one disadvantage of using Unicode. [4 marks]
Unicode was developed because ASCII could only represent 128 characters (or 256 in Extended ASCII), which is insufficient for the world's languages, symbols, and emoji. ASCII only covers English alphabet characters, digits, and basic punctuation. Advantage: Unicode supports over 143,000 characters from all writing systems worldwide, enabling global communication and multilingual documents in a single encoding standard. Disadvantage: Unicode requires more storage space per character than ASCII — up to 4 bytes compared to ASCII's 1 byte. This means text files are larger when using Unicode, though UTF-8 mitigates this by using 1 byte for ASCII characters.
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including character encoding: ASCII and Unicode for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including character encoding: ASCII and Unicode to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including character encoding: ASCII and Unicode, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.