A-level Computing/AQA/Paper 2/Fundamentals of data representation/ASCII and unicode





ASCII
ASCII normally uses 8 bits (1 byte) to store each character. However, the 8th bit is used as a check digit, meaning that only 7 bits are available to store each character. This gives ASCII the ability to store a total of 2^7 = 128 different values.

ASCII values can take many forms: Take a look at your keyboard and see how many different keys you have. The number should be 104 for a windows keyboard, or 101 for traditional keyboard. With the shift function valus (a, A; b, B etc.) and recognising that some keys have repeated functionality (two shift keys, the num pad). We roughly have 128 functions that a keyboard can perform.
 * Numbers
 * Letters (capitals and lower case are separate)
 * Punctuation (?/|\£$ etc.)
 * non-printing commands (enter, escape, F1)

If you look carefully at the ASCII representation of each character you might notice some patterns. For example: As you can see, a = 97, b = 98, c = 99. This means that if we are told what value a character is we can easily work out the value of subsequent or prior characters.

Without looking at the ASCII table above! If we are told that the ASCII value for the character '5' is 011 0101, what is the ASCII value for '8'.

We know that '8' is three characters after '5', as 5,6,7,8. This means that the ASCII value of '8' will be three bigger than that for '5': 011 0101 ASCII '5' +     011   011 1000  ASCII '8' Checking above this is the correct value.

If you are worried about making mistakes with binary addition, you can deal with the decimal numbers instead. Take the example where you are given the ASCII value of 'g', 110 0111, what is 'e'?

We know that 'e' is two characters before 'g', as e, f, g. This means that the ASCII value of 'e' will be two smaller than that for 'g'. 64 32 16 8  4  2  1  1  1  0  0  1  1  1 = 10310 = ASCII value of 'g' 103 - 2 = 10110 64 32 16 8  4  2  1  1  1  0  0  1  0  1 = 10110 = ASCII value of 'e'

88 - as it is 2 characters down in the alphabet

How many ASCII 'characters' does the following piece of text use: Hello Pete, ASCII rocks!

27 or 26. If you said 23 you'd be wrong because you must include the non-printing characters at the end of each line. Each end of line needs a EOL command, and a new line needs a carriage return (CR), making the text like so: Hello Pete,[EOL][CR] ASCII rocks![EOL]

For the Latin alphabet ASCII is generally fine, but what if you wanted to write something in Mandarin, or Hindi? We need another coding scheme!

You might have to use ASCII codes when reading from text files. To see what each ASCII code means we can use the folliwing function  which returns the ASCII code with denary value x.  Try out the following code to see the first 128 characters. What is special about character 10?

Unicode
The problem with ASCII is that it only allows you to represent a small number of characters (~128 or 256 for Extended ASCII). This might be OK if you are living in an English speaking country, but what happens if you live in a country that uses a different character set? For example: You can see that we quickly run into trouble as ASCII can't possibly store these hundreds of thousands of extra characters in just 7 bits. What we use instead is unicode. There are several versions of unicode, each with using a different number of bits to store data: With over a million possible characters we should be able to store every character from every language on the planet, take a look at these examples: You can find out more about unicode encoding on Wikipedia
 * Chinese characters 汉字
 * Japanese characters 漢字
 * Cyrillic Кири́ллица
 * Gujarati ગુજરાતી
 * Urdu اردو

100 0111 - as it is 3 characters further on in the alphabet

110 1101 - as it is 6 characters down in the alphabet

Each character only takes up 8 bits, meaning that storing data in ASCII may take up less memory than unicode

ASCII stores a much smaller character set than unicode, meaning that you are limited to the Latin character set and cannot represent characters from other languages.

2^7 = 128

unicode as it would allow you to display non Latin character sets such as Hindi and Cyrillic