2.2.1 - Character encoding with ASCII
To us, A, a and a blank space are obvious; to a computer, each needs an agreed binary pattern. In this lesson, you will use 7-bit ASCII to encode and decode short text, and explain why seven bits provide 128 different codes.
From characters to codes
A character is one item in text: for example, a letter, digit, punctuation mark or space. A computer represents a character as a number, which can then be written as a binary pattern. For two systems to interpret that pattern in the same way, they must use the same mapping between characters and numbers.
ASCII
ASCII stands for American Standard Code for Information Interchange. It is a standard coded character set that assigns each character in its set a unique numeric code represented by a 7-bit binary pattern.
Think of an ASCII table as an agreed lookup table:
character -> ASCII code value -> 7-bit binary pattern
For example, uppercase A has ASCII code value 65. Written as a 7-bit binary pattern, this is 1000001. A system decoding 1000001 with the same ASCII mapping recovers uppercase A.
The word encoding describes changing from the character to its code representation. Decoding follows the mapping in the opposite direction. ASCII is not a programming language and it is not encryption: its mapping is public, it uses no secret key, and its purpose is representation rather than secrecy.
Seven bits and 128 codes
One bit has two possible states: 0 or 1. With seven independent bit positions, the number of different patterns is:
2 x 2 x 2 x 2 x 2 x 2 x 2 = 2^7 = 128
Those 128 patterns run from 0000000 to 1111111. Their denary code values run from 0 to 127, inclusive. Zero counts as the first value, so 128 is the number of possible codes, while 127 is the largest code value.
Here is a small extract from the ASCII mapping:
| Character | Denary code | 7-bit ASCII pattern |
|---|---|---|
| space | 32 | 0100000 |
! | 33 | 0100001 |
0 | 48 | 0110000 |
A | 65 | 1000001 |
B | 66 | 1000010 |
Z | 90 | 1011010 |
a | 97 | 1100001 |
b | 98 | 1100010 |
Every pattern in the final column has exactly seven bits. The leading 0 in 0100001 matters because it shows the full width of the ASCII code. By contrast, 1000001 needs no leading zero because it already contains seven bits.
The 128 code positions are not all visible letters. ASCII includes uppercase and lowercase letters, digits, punctuation and space, as well as non-printing control codes. This is why it is more accurate to talk about characters and control codes than only "letters".
Seven bits create 128 different patterns, numbered from 0 to 127. Do not confuse the number of possible codes with the largest code value.
The selected standard is specifically 7-bit ASCII. The name ASCII should not be used here for an unspecified 8-bit extension.
Encoding and decoding text
To encode text with a supplied ASCII table:
- Read the characters from left to right.
- Look up each exact character, including its case.
- Write its 7-bit pattern and keep the groups in the same order.
Consider the four-character text A b!. The second character is a space and the third is lowercase b.
| Position | Character | Table lookup | 7-bit pattern |
|---|---|---|---|
| 1 | A | 65 | 1000001 |
| 2 | space | 32 | 0100000 |
| 3 | b | 98 | 1100010 |
| 4 | ! | 33 | 0100001 |
The encoded sequence is:
1000001 0100000 1100010 0100001
The spaces between groups are shown to make the character boundaries easy to inspect. Each group is still exactly seven bits.
To decode, reverse the process: split the sequence into 7-bit groups, look up each group, then join the characters in order. For example:
1000010 0100000 1100001
B space a
The decoded text is B a. Notice that uppercase B and lowercase b are different characters with different codes. A space also has its own code even though it normally appears blank.
Why a shared standard works
A binary pattern has meaning only when the system interpreting it knows which mapping was used. If a sender encodes uppercase A as 1000001 using ASCII, a receiver using the same ASCII table decodes the same pattern as uppercase A. The agreed standard therefore keeps the interpretation consistent between systems.
Encoding and decoding are opposite directions through the same mapping:
encode: character -> code value -> 7-bit pattern
decode: 7-bit pattern -> code value -> character
Because every 7-bit ASCII code has the same fixed width, a sequence can be separated into consecutive groups of seven bits when its character boundary is known. Changing the case, omitting a space or dropping a bit can select a different code, so exact characters and exact group width matter.
ASCII does not make text secret. It makes character representation consistent by giving systems the same public character-to-code mapping.
Use the complete chain in an explanation: name the character, map it to its 7-bit code, and explain that another system using the same standard can reverse the mapping.