Characters and ASCII

GCSE Computer Science (Edexcel) · Edexcel 1CP2 · 2.2.1 · Data › Data representation

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (Edexcel), marked as you tap.

Revision notes

How computers store text by giving every character a unique binary code, using 7-bit ASCII. Expect questions on how many characters 7 bits can represent, using the order of the codes to work out other codes, the number of bits needed for some text, and why ASCII is limited.

Key facts

  • Character set: the characters a computer can represent, each with a unique binary code
  • 7-bit ASCII: 27 = 128 characters (codes 0 to 127)
  • Codes are consecutive: A, B, C… and 0, 1, 2… follow each other
  • 'A' = 65, 'a' = 97, '0' = 48 (useful for checking)
  • Lower-case code = upper-case code + 32
  • Size of text in bits = number of characters × bits per character
  • Spaces, punctuation and control characters are characters too

Notes

Character sets and ASCII

  • Text is stored as a sequence of characters, and each character is given a unique binary code.
  • A character set is the list of characters a computer can represent, with the code for each one. Computers must use the same character set for text to be shown correctly.
  • ASCII uses 7 bits for each character, so it has 27 = 128 codes (0 to 127).
  • ASCII includes upper-case letters, lower-case letters, the digits 0 to 9, punctuation, symbols, the space, and control characters (such as new line) that are not printed.

How the codes are ordered

diagram
  • The codes are in order, so you can count on from a code you are given: if 'A' is 65 (100 0001), then 'E' is 69 (100 0101).
  • Upper-case letters come before lower-case letters: 'A' is 65 and 'a' is 97. Each lower-case letter's code is 32 more than its upper-case letter's code.
  • ABCDE6566676869abcde979899100101charactercodecharactercode+32
    Count on from a code you know. Each lower-case letter is 32 more than its capital
  • Digit characters have codes too: '0' is 48, up to '9', which is 57.
  • The character '5' (code 53) is not the same as the number 5. A number typed in as text must be converted (e.g. with int() in Python) before you can calculate with it.
  • You don't need to memorise the whole table: questions normally give you a code or a table to work from.

Storing text

diagram
  • Bits for some text = number of characters × 7. Spaces and punctuation marks are characters too.
  • Example: 'Hello Sam' has 9 characters (including the space), so it needs 9 × 7 = 63 bits.
  • HelloSamspace1234567899 characters × 7 bits = 63 bits
    Every character counts, including spaces and punctuation
  • Limitation: 128 codes are not enough for accented letters (such as é), other alphabets (such as Greek, Arabic or Chinese) or emoji. Character sets that use more bits per character can represent many more characters, but every character then takes up more space. Grade 7+ only

How to answer each type of question

Work out a code or decode a message

1 to 2 marksGrade 6
  1. Find the code you are given as a starting point (e.g. 'K' = 75).
  2. Count on or back through the alphabet or the digits.
  3. Convert between denary and 7-bit binary if the question uses binary codes.

Example. The ASCII code for 'K' is 75.
(a) State the denary ASCII code for 'N'.
(b) Give the 7-bit binary ASCII code for 'H'.

Show the model answerHide the model answer
(a) 78
(1)
(b) 'H' is 3 letters before 'K', so its code is 72 = 100 1000
(1)

Calculate the number of bits needed to store text

1 to 2 marksGrade 5
  1. Count every character, including spaces and punctuation.
  2. Multiply by the number of bits per character (7 for 7-bit ASCII).
  3. Give the unit (bits).

Example. A text message says See you at 6!
Calculate the number of bits needed to store the message using 7-bit ASCII.

Show the model answerHide the model answer
13 characters, including the 3 spaces and the !
(1)
13 × 7 = 91 bits
(1)

Explain a limitation of 7-bit ASCII

2 marksGrade 7
  1. Give the number of characters ASCII can represent (128).
  2. Name what is missing in the context (accented letters, another alphabet, emoji).
  3. If there is room, say that more bits per character are needed.

Example. A travel company wants its booking app to display place names in Greek and in Japanese.
Explain why 7-bit ASCII is not suitable.

Show the model answerHide the model answer
7-bit ASCII can only represent 128 different characters (1). It has no codes for Greek or Japanese characters, and Japanese alone needs thousands of characters, far more than 128 (1).

Identify the number of characters (multiple choice)

1 markGrade 4
  1. Work out 2n for the number of bits given.
  2. Watch out for the option that is one less (the largest code, not the number of codes).

Example. Identify the number of different characters that can be represented using 7-bit ASCII.
A 7
B 64
C 127
D 128

Show the model answerHide the model answer
D, 128 (27) (1)

Don’t lose marks

  • Forgetting to count spaces and punctuation when working out the size of some text.
  • Saying 7-bit ASCII has 127 characters. There are 128 codes (0 to 127).
  • Mixing up upper and lower case: 'h' does not have the same code as 'H'.
  • Treating the digit character '9' as if it were the number 9 in a calculation.
  • Using 8 bits per character when the question says 7-bit ASCII (or 7 bits when it says each character is stored in one byte).

More tips

Memory tricks

  • 'A' is 65 and 'a' is 97: capitals first, then lower case, 32 apart.
  • Upper-case letter code = 64 + its position in the alphabet (A = 1 gives 65; Z = 26 gives 90). Lower case is 96 + its position.
  • Digit character code = 48 + the digit, so '7' is 55.
  • When counting characters, point at each one in turn, including every space and punctuation mark.

Exam technique

  • Use the codes the question gives you and show your counting, e.g. 'K = 75, so N = 78'.
  • In 'explain' questions about ASCII's limits, give the number (128) and a specific example of a missing character.
  • Check how many bits per character the question uses: 7 for 7-bit ASCII, 8 if it says one byte per character.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    Define a character setA list of the characters a computer can represent, each with its own unique binary code.
  2. Grade 4
    State how many characters 7-bit ASCII represents7 bits give 27 = 128 different codes, from 0 to 127.
  3. Grade 5
    Work out codes from a given codeLetters and digits have consecutive codes, so if 'A' is 65 then 'D' is 68.
  4. Grade 5
    Calculate the bits needed to store textMultiply the number of characters (including spaces) by 7 for 7-bit ASCII.
  5. Grade 6
    Convert between binary codes and charactersConvert the 7-bit code to denary, then count on or back from a code you know.
  6. Grade 7
    Explain the limitations of 7-bit ASCII128 codes cannot include accented letters, other alphabets or most symbols and emoji.
  7. Grade 8
    Explain why '7' and 7 are stored differentlyThe character '7' is stored as its ASCII code, 55 (011 0111), not as the binary number 7 (000 0111).

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

State the number of different characters that can be represented using 7-bit ASCII.
128
State why each character in a character set must have a unique binary code.
So every code stands for exactly one character and is always displayed correctly.
The ASCII codes for 'a' to 'z' are 97 to 122, and the codes for 'A' to 'Z' are 65 to 90. This Python program changes the letters in a word.
01 word = input("Enter a word: ")
02 result = ""
03 for letter in word:
04     code = ord(letter)
05     if code >= 97 and code <= 122:
06         code = code - 32
07     result = result + chr(code)
08 print(result)
State the output when the user enters Hi5x.
HI5X
A program sorts words into order by comparing the ASCII codes of their characters, one character at a time from the left. The ASCII code for 'A' is 65, for 'a' is 97 and for '0' is 48. State the ASCII codes for 'Z' and for 'a'.
'Z' = 90, 'a' = 97
In 7-bit ASCII, the character '0' has the code 48, and the characters '0' to '9' have consecutive codes. State the ASCII code for the character '7'.
55
Write the letters A, B and C in the order in which the steps happen.
B, C, A

Sample questions

Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.

Question 1Easy3 marks
(a) State the number of different characters that can be represented using 7-bit ASCII.
[1]
(b) The ASCII code for 'M' is 77. State the ASCII code for 'P'.
[1]
(c) Identify the best description of a character set.
[1]
  • A list of characters that a computer can represent, each with its own unique binary code
  • The keys on a computer keyboard
  • A style of lettering used to display text on a screen
  • The number of characters in a string
Show the answer and mark scheme
(a) Answer: 128
  • 128 (27)
(b) Answer: 80
  • 80
(c) Answer: A list of characters that a computer can represent, each with its own unique binary code
Question 2Medium4 marks
In ASCII, the character '0' has the code 48 (011 0000 in 7-bit binary). The digits '0' to '9' have consecutive codes.
(a) Give the 7-bit ASCII code for the character '6'.
[1]
(b) A user types 25 into a text box. The program stores it as a string of two characters.
Give the denary ASCII codes that are stored.
[1]
(c) Explain why these codes are different from the binary representation of the integer 25.
[2]
Show the answer and mark scheme
(a) Answer: 011 0110
  • 011 0110 (54)
(b) Answer: 50, 53
  • 50 and 53 (in that order)
(c) Answer: The string stores a code for each digit character; the integer 25 is stored as the binary number 0001 1001, so the text must be converted before doing arithmetic.
  • each digit is stored as a separate character code ('2' = 50, '5' = 53), not as a number
  • the integer 25 would be stored as its binary value (0001 1001), so the string must be converted to an integer, e.g. with int(), before it can be used in calculations
Question 3Hard4 marks
In ASCII, 'G' has the code 100 0111 (71) and 'g' has the code 110 0111 (103).
(a) State the difference between the denary codes of 'G' and 'g'.
[1]
(b) Explain how a program could use ASCII codes to convert any lower-case letter to upper case.
[2]
(c) Give a reason why the program should check that a character is a lower-case letter before converting it.
[1]
Show the answer and mark scheme
(a) Answer: 32
  • 32
(b) Answer: Subtract 32 from its code (clear the 32 bit), because each lower-case code is 32 more than the upper-case code.
  • subtract 32 from the ASCII code of the letter / change the bit with place value 32 from 1 to 0
  • because the code of every lower-case letter is exactly 32 more than the code of the matching upper-case letter
(c) Answer: Otherwise other characters are corrupted, e.g. 'A' (65) would become '!' (33).
  • other characters would be changed into the wrong characters, e.g. 'A' (65) would become '!' (33)

More in Data representation

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.