Fundamentals of Data Representation: ASCII and unicode
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.
There is also extended ASCII that uses the 8th bit to store data, allowing for a much larger character set, but for the exam you'll probably be fine with 7 bit parity ASCII |
ASCII values can take many forms:
- Numbers
- Letters (capitals and lower case are separate)
- Punctuation (?/|\£$ etc.)
- non-printing commands (enter, escape, F1)
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.
|
|
|
|
If you look carefully at the ASCII representation of each character you might notice some patterns. For example:
Binary | Dec | Hex | Glyph |
---|---|---|---|
110 0001 | 97 | 61 | a |
110 0010 | 98 | 62 | b |
110 0011 | 99 | 63 | c |
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.
Example: ASCII 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' |
Exercise: ASCII Without using the crib table (you won't get it in the exam!) answer the following questions: The ASCII code for the letter 'Z' is 90(base10), what is the letter 'X' stored as Answer:
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! Answer: 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!
Extension: Coding ASCII 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 For x = 0 To 127
Console.WriteLine("ASCII for " & x & " = " & ChrW(x))
Next
Console.ReadLine()
|