KS48525 3.3.4 20 min read 10 question quiz 11 exam marks

Character Encoding

Character sets, ASCII and Unicode, why the codes for letters and digits are in order, and why the character '7' is not the number 7.

Character sets

A computer stores only numbers, so text has to be turned into numbers first. A character set is an agreed table giving every character a unique binary code.

Two machines can only exchange text if they use the same character set. Send text encoded in one set and display it using another and you get the wrong characters, which is exactly what the box symbols and question marks in badly handled text are.

A character set is an agreement, not a property of the machine

There is nothing about the number 65 that makes it an A. It is an A because everybody agreed it would be.

ASCII

ASCII is a 7 bit character set, so it has 2 to the power 7, which is 128 codes, numbered 0 to 127. That covers the upper and lower case English alphabet, the digits, punctuation and a set of control characters such as carriage return.

Extended ASCII uses 8 bits and so has 256 codes, adding accented letters and box drawing characters.

The codes are in order, and that is useful

  • 'A' is 65, so 'B' is 66 and 'Z' is 90.
  • 'a' is 97, so lower case is always the upper case code plus 32.
  • '0' is 48, so the numeric value of a digit character is its code minus 48.

Those relationships get examined, because they let you do things with arithmetic instead of a lookup table.

OCR reference language
code ← CHAR_TO_CODE(letter)
IF code ≥ 65 AND code ≤ 90 THEN
    letter ← CODE_TO_CHAR(code + 32)
ENDIF
'7' is not 7

The character '7' has the ASCII code 55. The integer 7 is stored as the value seven. They are different things, which is why input has to be cast before it can be used in arithmetic.

From a character to the bits storedHow text is stored. Each character is given a number by a character set: in ASCII, capital H is 72 and lower case i is 105. That number is then written in binary, so H becomes 01001000 and i becomes 01101001. ASCII uses seven bits and is normally stored in one byte per character, so the two character string Hi takes two bytes, and the size of any text is simply the number of characters multiplied by the bytes used for each. ASCII has only 128 codes, enough for English letters, digits and punctuation, so Unicode extends the same idea with up to 32 bits and over 140,000 codes, covering every writing system in use as well as emoji.Storing the text "Hi"Hcharactericharacter72ASCII code105ASCII code72105010010008 bits011010018 bits010010001 byte011010011 byte2 characters x 1 byte = 2 bytesASCII7 bits, 128 codesEnglish letters,digits, punctuationUNICODEUp to 32 bitsOver 140,000 codesEvery writing system,plus emoji
A character set is only a lookup table from characters to numbers. Everything else follows from that one idea.
Characters stored as numbers, and the three character sets ASCII, extended ASCII and UnicodeComputers store text as numbers. A character set is the agreed list that says which number means which character. In ASCII the capital letter A is 65, which is 0100 0001 in binary, B is 66, a lower case a is 97 and the character 7 is 55. The codes run in order, so B is always one more than A, and a lower case letter is 32 higher than its capital. The character 7 is text with code 55, which is different from the number 7. Below are three character sets. ASCII uses 7 bits per character and so holds 128 characters. Extended ASCII uses 8 bits and holds 256, adding accents and symbols such as é, ñ and £. Unicode uses up to 32 bits and holds over a million characters, covering every writing system and emoji, with the first 128 codes kept the same as ASCII.Every character is stored as a numberA6501000001B6601000010a970110000175500110111Codes run in order:A 65, B 66, C 67 ...Lower case is 32 higher:a is 97, A is 65The character 7 is code 55,not the number 7Three character setsASCII7 bits each128 charactersAz5?Extended ASCII8 bits each256 characterséñ£©Unicodeup to 32 bits eachover a millionΩЖ123
  1. Each character has a code number, and the computer stores that number in binary.
  2. The codes run in order, so you can work out C from A. The character 7 is not the number 7.
  3. More bits per character means more characters: 128, then 256, then over a million.
A character set is just an agreed table of numbers. Unicode is needed because 256 characters cannot cover the world's languages.

Unicode

ASCII was designed for English. It has no code for Greek, Arabic, Chinese, Hindi, or for the symbols used in mathematics and music.

Unicode solves this by using far more bits per character, which gives it room for well over a million codes, enough for every writing system in current use and a great many that are not.

ASCIIUnicode
Bits per character7, or 8 for extendedTypically 8 to 32, variable
Number of characters128, or 256 extendedOver a million available
Languages coveredEnglish and a little moreEffectively all of them
Storage per characterSmallLarger for characters outside the basic set

The trade off is size. A document in Unicode can take more storage than the same document in ASCII, because some characters need more bytes. The first 128 Unicode codes are deliberately identical to ASCII, so plain English text costs the same in both and older files still open correctly.

The comparison answer

Unicode represents far more characters, so it supports languages ASCII cannot, at the cost of using more bits per character and therefore more storage. Both halves are needed for full marks.

Key terms you must be able to define

Character set
An agreed table giving every character a unique binary code.
ASCII
A 7 bit character set with 128 codes, covering English letters, digits, punctuation and control characters.
Extended ASCII
An 8 bit version of ASCII with 256 codes.
Unicode
A character set using more bits per character, able to represent over a million characters from every writing system.
Character code
The number a character set assigns to a particular character.
Control character
A character code that causes an action, such as a new line, rather than printing a symbol.

Flashcards

Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.

DefineCharacter set
AnswerAn agreed table giving every character a unique binary code.
DefineASCII
AnswerA 7 bit character set with 128 codes, covering English letters, digits, punctuation and control characters.
DefineExtended ASCII
AnswerAn 8 bit version of ASCII with 256 codes.
DefineUnicode
AnswerA character set using more bits per character, able to represent over a million characters from every writing system.
DefineCharacter code
AnswerThe number a character set assigns to a particular character.
DefineControl character
AnswerA character code that causes an action, such as a new line, rather than printing a symbol.

Reach the top grade on this topic

The examinable subtlety here is the difference between a character and the value it looks like, and the strongest answers make it explicit.

They say what is actually stored. Not "the letter A is stored" but "the code 65 is stored as 01000001, and software uses the same character set to draw an A when it is displayed".

They quantify Unicode's cost. A grade 9 comparison names the benefit and the price in the same sentence: more characters, therefore more languages, therefore more bits per character and a larger file.

They exploit the ordering. When a question asks how a program could convert case or find the numeric value of a digit character, the answer is arithmetic on the code, and saying so shows you understand what a character set is.

Mistakes that cost marks here

Avoid these
  • Saying ASCII has 256 characters. Standard ASCII is 7 bit and has 128; extended ASCII has 256.
  • Treating the character '5' as the number 5.
  • Saying Unicode replaced ASCII entirely. The first 128 Unicode codes are the same as ASCII by design.
  • Claiming Unicode always uses more storage. For plain English text in a variable width encoding it uses the same.
  • Forgetting that both machines must use the same character set for text to display correctly.
Fun fact

The ASCII code for a capital letter and its lower case partner always differ by exactly 32, which is one bit. That was deliberate: it let 1960s hardware change case by flipping a single bit rather than by looking anything up.

Knowledge check

0 of 10 answered
1How many different characters can a 7 bit character set represent?
2If 'A' has the code 65, what is the code for 'D'?
3If 'A' is 65 and 'a' is 97, what is the difference between an upper case and lower case code?
4What is the main advantage of Unicode over ASCII?
5What is the main disadvantage of Unicode compared with ASCII?
6Why can the same file appear as the wrong characters on a different computer?
7What is actually stored when a computer saves the letter K?
8If '0' has the code 48, how would a program get the numeric value of a digit character?
9What is a control character?
10Why are the first 128 Unicode codes the same as ASCII?
Score 0 / 10

Exam-style questions

Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.

11 marks
1State what is meant by a character set.1 mark
State

Mark scheme0 / 1
  • LAn agreed set of codes giving every character a unique binary value
Model answer

A character set is an agreed table that gives every character a unique binary code, so that text can be stored and exchanged as numbers.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
2A character set uses 8 bits per character. Calculate how many different characters it can represent.2 marks
Calculate

Mark scheme0 / 2
  • LShows that the number of combinations is 2 to the power of the number of bits
  • LGives 256
Model answer

Each bit can be 0 or 1, so eight bits give 2 multiplied by itself eight times, which is 2 to the power 8. That is 256, so the character set can represent 256 different characters, numbered 0 to 255.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
3Explain one advantage and one disadvantage of using Unicode rather than ASCII.4 marks
Explain

Mark scheme0 / 4
  • LUnicode can represent far more characters
  • LSo it supports languages and symbols ASCII has no codes for
  • LUnicode uses more bits per character for characters outside the basic set
  • LSo files can take more storage and take longer to transmit
Model answer

The advantage is coverage. Unicode has room for well over a million codes, so it can represent Chinese, Arabic, Greek, mathematical symbols and emoji, none of which ASCII has any code for at all. That makes it possible to store and exchange text in any language rather than only in English. The disadvantage is size. Characters outside the basic Latin set need more than one byte, so a document containing them takes more storage and more bandwidth to send than the equivalent ASCII file would, if ASCII could represent it at all.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
4A program reads the character '9' from a keyboard and adds 1 to it, expecting 10. Explain why this does not work and how it should be corrected.4 marks
Explain

Mark scheme0 / 4
  • LThe keyboard returns the character '9', not the number 9
  • LThe character is stored as its character code, for example 57
  • LAdding 1 either produces the next character or joins values rather than adding them
  • LThe character must be converted to an integer first, for example with STRING_TO_INT
Model answer

What arrives from the keyboard is the character '9', which is stored as its character set code rather than as the value nine. In ASCII that code is 57. Adding one to it therefore gives 58, which is the code for a colon, or in a language that treats the value as a string it joins the two values rather than adding them. Either way the result is not ten. The correction is to convert the character to an integer before doing arithmetic, for example value ← STRING_TO_INT(input), after which adding one gives ten as intended.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
Machine mark 0 / 11