A code is a system of rules to convert information from one form to another. When we convert given information into another representation, we are encoding. When we convert back to the original representation, we are decoding. We represent the rules for encoding and decoding by functions. To be able to recover the original information through decoding, the encoding function must be invertible.
In this section we convert text into a sequence of numbers. Commonly used character encodings are ASCII (American Standard Code for Information Interchange) and Unicode. ASCII uses 128 printable and control characters and was standardized in 1963 by ASA (American Standards Association). Unicode can handle the characters in most of the world’s writing systems.
We use a simpler code that only encodes the characters in the set
Recall that when we write texts with the characters in \(\A\) we write \(\cspace\) instead of the character space.
To convert text into a sequence of numbers we use the encoding function \(C\) and its inverse the decoding function \(C^{-1}\) given in Figure 8.1. In practice we only need one of these tables, as instead of explicitly writing down the table for \(C^{-1}\) we can also read the table for \(C\) from right to left.
\(x\)
\(C(x)\)
\(\cspace\)
0
\(\mathtt{a}\)
1
\(\mathtt{b}\)
2
\(\mathtt{c}\)
3
\(\mathtt{d}\)
4
\(\mathtt{e}\)
5
\(\mathtt{f}\)
6
\(\mathtt{g}\)
7
\(\mathtt{h}\)
8
\(\mathtt{i}\)
9
\(\mathtt{j}\)
10
\(\mathtt{k}\)
11
\(\mathtt{l}\)
12
\(\mathtt{m}\)
13
\(\mathtt{n}\)
14
\(\mathtt{o}\)
15
\(\mathtt{p}\)
16
\(\mathtt{q}\)
17
\(\mathtt{r}\)
18
\(\mathtt{s}\)
19
\(\mathtt{t}\)
20
\(\mathtt{u}\)
21
\(\mathtt{v}\)
22
\(\mathtt{w}\)
23
\(\mathtt{x}\)
24
\(\mathtt{y}\)
25
\(\mathtt{z}\)
26
\(y\)
\(C^{-1}(y)\)
0
\(\cspace\)
1
\(\mathtt{a}\)
2
\(\mathtt{b}\)
3
\(\mathtt{c}\)
4
\(\mathtt{d}\)
5
\(\mathtt{e}\)
6
\(\mathtt{f}\)
7
\(\mathtt{g}\)
8
\(\mathtt{h}\)
9
\(\mathtt{i}\)
10
\(\mathtt{j}\)
11
\(\mathtt{k}\)
12
\(\mathtt{l}\)
13
\(\mathtt{m}\)
14
\(\mathtt{n}\)
15
\(\mathtt{o}\)
16
\(\mathtt{p}\)
17
\(\mathtt{q}\)
18
\(\mathtt{r}\)
19
\(\mathtt{s}\)
20
\(\mathtt{t}\)
21
\(\mathtt{u}\)
22
\(\mathtt{v}\)
23
\(\mathtt{w}\)
24
\(\mathtt{x}\)
25
\(\mathtt{y}\)
26
\(\mathtt{z}\)
Figure8.1.Tables that specify the encoding function \(C:\A\to\Z_{27}\) and its inverse the decoding function \(C^{-1}:\Z_{27}\to\A\text{.}\) The character space is represented by \(\cspace\text{.}\)
When encoding words or longer segments of text we apply the encoding function character by character and thus obtain a sequence of numbers (to be more exact a sequence of elements of \(\Z_{27}\)) which we separate by commas.
In the video in Figure 8.2 we give a detailed description of the use of the encoding function \(C\) and its inverse the decoding function \(C^{-1}\text{.}\)
In the following we give examples on how to apply the encoding and decoding functions.
Problem8.3.Encoding a word.
Encode the word \(\mathtt{cookies}\) with the encoding function \(C\text{.}\)
Solution.
We evaluate the encoding function \(C\) at the characters in the word \(\mathtt{cookies}\text{.}\) We have:
To obtain the text encoded in a sequence of numbers (to be more exact a sequence of elements of \(\Z_{27}\)) we apply the decoding function \(C^{-1}\) to each number.
Problem8.4.Decoding.
Decode \(20, 15, 15, 0, 5, 1, 19, 25\) with the decoding function \(C^{-1}\text{.}\)
Thus we obtain the words \(\mathtt{too{\cspace}easy}\text{.}\)
In the following problems we do not explicitly evaluate the encoding function \(C\) or its inverse the decoding function \(C^{-1}\) anymore, since the encoding and decoding process is a simple lookup in Figure 8.1.
Problem8.5.Encoding a sentence.
Encode the text 1
from the poem Devotions upon Emergent Occasions by John Donne, 1624