University of Wales Trinity Saint David
ACCA4021 - Introduction to Computer Systems

ASCII to Binary

Overview

When you type a letter on a keyboard, the computer cannot store the shape of the letter. It can only store bits. So every character is given a number, and that number is stored in binary. The most widely used numbering scheme is called ASCII. In this worksheet you will look up ASCII codes and convert them to binary yourself, encode whole words, decode a hidden message, and find the patterns that make ASCII easy to work with. There is no programming in this worksheet. Work through it with a pen and paper, and use the Check Answers buttons to test yourself as you go.

Learning Objectives

By the end of this lab you will be able to:

Concept

A character set is an agreed list that gives every character (letters, digits, punctuation, the space and some control codes) its own number. As long as two computers use the same character set, the number 65 sent by one will be shown as A by the other.

ASCII (American Standard Code for Information Interchange) is a character set that uses 7 bits, giving 27 = 128 codes, numbered 0 to 127. Codes 0 to 31 are invisible control codes (such as new line and tab). Codes 32 to 126 are the printable characters you can see on a keyboard. In practice each character is stored in a full byte (8 bits), with the extra leftmost bit set to 0.

Turning text into binary is a two-step process for each character: look up its ASCII code in the table, then convert that code from denary to 8-bit binary. Decoding is the same two steps in reverse.

Tasks

Task 1 - Characters are numbers

Here is the table of printable ASCII characters. For each character it gives the ASCII code in denary (Den) and in hex. Keep this table open for the rest of the worksheet.

CharDenHexCharDenHexCharDenHex
space3220@6440`9660
!3321A6541a9761
"3422B6642b9862
#3523C6743c9963
$3624D6844d10064
%3725E6945e10165
&3826F7046f10266
'3927G7147g10367
(4028H7248h10468
)4129I7349i10569
*422AJ744Aj1066A
+432BK754Bk1076B
,442CL764Cl1086C
-452DM774Dm1096D
.462EN784En1106E
/472FO794Fo1116F
04830P8050p11270
14931Q8151q11371
25032R8252r11472
35133S8353s11573
45234T8454t11674
55335U8555u11775
65436V8656v11876
75537W8757w11977
85638X8858x12078
95739Y8959y12179
:583AZ905Az1227A
;593B[915B{1237B
<603C\925C|1247C
=613D]935D}1257D
>623E^945E~1267E
?633F_955FDEL1277F

Look at the table and answer the questions below. You do not need to convert anything to binary yet.

The ASCII code for A is

The ASCII code for a is

The ASCII code for the digit 0 is

The ASCII code for a space is

Which character has ASCII code 72?

A is 65, so 72 is seven letters on: H. Lower case h is 104.

Which character has ASCII code 105?

Lower case starts at a = 97, so 105 is eight letters on: i. Capital I is 73.

Why does a space need its own ASCII code?

If spaces were not stored, "a cat" and "acat" would be identical. A space is code 32.

Task 2 - The conversion process

To turn a character into binary, follow these steps every time.

  1. Look up the character in the ASCII table to find its code in denary.
  2. Convert the denary code to 8-bit binary.
  3. Check your answer by adding the column values back up.

For step 2, use the 8-bit column values and ask, from the left, "does this column fit into what is left?".

1286432168421

Because ASCII only goes up to 127, the 128 column will always be 0.

Worked example: the letter K

Step 1. Look up K in the table. Its code is 75.

Step 2. Convert 75 to binary.

Convert 75

128 no                          bit = 0
 64 fits      75 -  64 =  11    bit = 1
 32 no                          bit = 0
 16 no                          bit = 0
  8 fits      11 -   8 =   3    bit = 1
  4 no                          bit = 0
  2 fits       3 -   2 =   1    bit = 1
  1 fits       1 -   1 =   0    bit = 1

Answer    01001011

Step 3. Check: 64 + 8 + 2 + 1 = 75. Correct, so K is stored as 01001011.

Another route via hex. If you have done the Binary to Hexadecimal worksheet, you can also use the hex column. K is hex 4B. Turn each hex digit into a nibble: 4 = 0100 and B = 1011, giving 0100 1011. Same answer. Use whichever method you find easier, and use the other one to check.

Task 3 - Convert single characters

Work out each of these on paper using the three steps from Task 2. Write the ASCII code, then the 8-bit binary. Only press Check Answers once you have done them all.

A has ASCII code which is in binary

M has ASCII code which is in binary

z has ASCII code which is in binary

5 has ASCII code which is in binary

? has ASCII code which is in binary

(space) has ASCII code which is in binary

Task 4 - Encode a word

A word or sentence is stored as one byte per character, one after another, in order. Here is how the word Cat is stored.

CharacterASCII code (denary)Binary (8 bits)
C6701000011
a9701100001
t11601110100

In memory these three bytes sit side by side: 01000011 01100001 01110100

Notice that upper case C and lower case a come from different parts of the table. Case matters. Cat and cat are stored differently.

Now encode the word Data, one character at a time.

D has ASCII code which is in binary

a has ASCII code which is in binary

t has ASCII code which is in binary

a has ASCII code which is in binary

Now encode your own initials, including a full stop after each one (for example J.S.). Write the bytes on paper. You can check them with the encoder in Task 7.

Task 5 - Decode a hidden message

Decoding is the same process backwards. For each byte, convert the binary to denary by adding up the column values, then look up the character in the ASCII table.

Worked example. 0100 1111

64 + 8 + 4 + 2 + 1 = 79. Code 79 in the table is O.

Decode this four-character message.

0100 0010    0111 1001    0111 0100    0110 0101

Byte 1 01000010 is code which is the character

Byte 2 01111001 is code which is the character

Byte 3 01110100 is code which is the character

Byte 4 01100101 is code which is the character

The hidden word is

Look again at byte 1. Is the first letter of the hidden word upper case or lower case?

Byte 1 is code 66. Capitals run from 65 to 90 and small letters from 97 to 122.

Task 6 - Spotting the patterns

ASCII was designed with some useful patterns. Once you know them you can work out many codes without the table.

Pattern 1. The alphabet is in order. A is 65, B is 66, and so on up to Z at 90. Lower case runs from a (97) to z (122). If you know A = 65 you can count on to any capital letter.

Pattern 2. Upper and lower case differ by 32. Compare these two rows.

CharCodeBinary
A6501000001
a9701100001

The only difference is the bit in the 32 column. Switching that one bit switches between capital and small letters. This is how a computer can change the case of text very quickly.

Pattern 3. Digits are not stored as their own value. The character 7 is code 55, which is 0011 0111. The right-hand nibble is 0111, which is 7, and every digit character starts with 0011.

G is 01000111. Without the table, g in binary is

Q is 01010001. Without the table, q in binary is

Count on from A = 65. The code for J is

Using pattern 3, the character 9 in binary is

Using pattern 3, the character 2 in binary is

The character "7" is stored as 00110111, but the number 7 is 00000111. Why are they different, and why does it matter?

The character "7" is a symbol to be displayed, so it is stored using its ASCII code, 55. The number 7 is a value to do arithmetic with, so it is stored as ordinary binary. If a program reads a digit typed by the user, it receives the character code, and must convert it (for example by subtracting 48) before it can do maths with it. Treating "7" + "1" as text gives "71", not 8.

Task 7 - Check your work with the encoder

Now that you can do the conversion by hand, use the encoder below to check your answers. Type any text (up to 16 characters) and it shows each character, its ASCII code and its 8 bits, and then the full stream of bits as it would sit in memory.

  1. Type the initials you encoded in Task 4 and compare with your paper answers.
  2. Type Cat and then cat. Which bit changes?
  3. Type a word with a space in it. What does the space become?
  4. Type a £ sign or an accented letter such as é. What happens?

Task 8 - How much space does text take?

Because each ASCII character is stored in one byte, working out storage is simple.

bits=number of characters×8\text{bits} = \text{number of characters} \times 8

Remember that spaces and punctuation are characters too. Hello, world! has 13 characters, so it needs 104 bits, or 13 bytes.

Computer needs bits

Hi there needs bits

ACCA4021 rocks! needs bytes

A text message is stored in 1,280 bits. It contains ASCII characters

Task 9 - The limits of ASCII

ASCII was designed in the 1960s for English. With only 128 codes there is no room for characters such as £, é, ŵ, Chinese or Arabic script, or emoji. You saw this in Task 7, where the encoder could not convert them.

Extended ASCII used the spare eighth bit to add another 128 characters (codes 128 to 255), but different countries filled those codes with different characters, so text often came out garbled when it moved between computers.

The modern answer is Unicode, which gives a unique number to over 150,000 characters from almost every writing system. The most common way of storing Unicode, UTF-8, uses one byte for the first 128 characters and two to four bytes for everything else. Those first 128 characters are exactly the same as ASCII, so everything you have learned in this worksheet still applies to plain English text today.

How many different characters can standard 7-bit ASCII represent?

2 to the power of 7 is 128, numbered 0 to 127.

Why is ASCII not enough for a Welsh language website?

A file of plain English text is saved as UTF-8. How does it compare with the same text saved as ASCII?

Task 10 - Check your understanding

What is the ASCII code for the letter "E"?

A is 65, so E (four letters on) is 69. 101 is lower case e.

Which byte stores the character "d"?

"d" is code 100 = 64 + 32 + 4 = 01100100. 01000100 is capital D.

The byte 00100001 is decoded using ASCII. Which character is it?

32 + 1 = 33, which is "!".

What are the two steps to turn a character into binary?

Show your full working to convert the word "OK" into binary.

O has ASCII code 79. 79 = 64 + 8 + 4 + 2 + 1, so O = 01001111.

K has ASCII code 75. 75 = 64 + 8 + 2 + 1, so K = 01001011.

"OK" is stored as 01001111 01001011, which is 2 bytes or 16 bits.

Explain why two computers must agree on a character set before they can exchange text.

A computer only stores and sends numbers in binary. The character set is the agreement that says which number means which character. If the sender and receiver use different character sets, the same number would be shown as a different character, and the text would come out wrong or garbled.