Skip to main content
Logo image

Section 8.1 Character Encoding

A code is a system of rules for changing information from one form to another. When we change given information into another representation, we are encoding. When we change it back to the original representation, we are decoding. We describe the rules for encoding and decoding with functions. The encoding function must be invertible so decoding can recover the original information.
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. It encodes only the characters in the set
\begin{equation*} \A=\{\cspace,\mathtt{a},\mathtt{b},\mathtt{c},\dots,\mathtt{z}\} \end{equation*}
as a sequence of numbers in
\begin{equation*} \Z_{27}=\{0,1,2,\ldots,26\}. \end{equation*}
Recall that when we write text with the characters in \(\A\text{,}\) we use \(\cspace\) instead of the space character.
To convert text into a sequence of numbers, we use the encoding function \(C\) and its inverse, the decoding function \(C^{-1}\text{,}\) given in Figure 8.1.1. In practice, we need only one table. Instead of writing a separate table for \(C^{-1}\text{,}\) we can 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}\)
Figure 8.1.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{.}\)
To encode words or longer parts of text, we apply the encoding function character by character. This gives a sequence of numbers, or more exactly, a sequence of elements of \(\Z_{27}\text{.}\) We separate these numbers with commas.
The video in Figure 8.1.2 explains in detail how to use the encoding function \(C\) and its inverse, the decoding function \(C^{-1}\text{.}\)
Figure 8.1.2. Character encoding by Matt Farmer and Stephen Steward
In the following we give examples on how to apply the encoding and decoding functions.

Problem 8.1.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:
\begin{align*} C(\mathtt{c})\amp=3\\ C(\mathtt{o})\amp=15\\ C(\mathtt{k})\amp=11\\ C(\mathtt{i})\amp=9\\ C(\mathtt{e})\amp=5\\ C(\mathtt{s})\amp=19 \end{align*}
Thus \(\mathtt{cookies}\) is encoded as the numbers
\begin{equation*} 3,15,15,11,9,5,19 \end{equation*}
To recover text from a sequence of numbers, or more exactly, from a sequence of elements of \(\Z_{27}\text{,}\) we apply the decoding function \(C^{-1}\) to each number.

Problem 8.1.4. Decoding.

Decode \(20, 15, 15, 0, 5, 1, 19, 25\) with the decoding function \(C^{-1}\text{.}\)
Solution.
We have
\begin{align*} C^{-1}(20)\amp=\mathtt{t}\\ C^{-1}(15)\amp=\mathtt{o}\\ C^{-1}(0)\amp=\cspace\\ C^{-1}(5)\amp=\mathtt{e}\\ C^{-1}(1)\amp=\mathtt{a}\\ C^{-1}(19)\amp=\mathtt{s}\\ C^{-1}(25)\amp=\mathtt{y} \end{align*}
Thus we obtain the words \(\mathtt{too{\cspace}easy}\text{.}\)
In the following problems, we no longer explicitly evaluate the encoding function \(C\) or its inverse, the decoding function \(C^{-1}\text{.}\) The encoding and decoding process is a simple lookup in Figure 8.1.1.

Problem 8.1.5. Encoding a sentence.

Encode the text
 1 
from the poem Devotions upon Emergent Occasions by John Donne, 1624
\begin{align*} \amp\mathtt{and{\cspace}therefore{\cspace}never{\cspace}send{\cspace}to{\cspace}know{\cspace}for{\cspace}}\\ \amp\mathtt{whom{\cspace}the{\cspace}bell{\cspace}tolls{\cspace}it{\cspace}tolls{\cspace}for{\cspace}thee} \end{align*}
with the function \(C\) from Figure 8.1.1.
Solution.
We obtain the sequence of elements of \(\Z_{27}\text{:}\)
1, 14, 4, 0, 20, 8, 5, 18, 5, 6, 15, 18, 5, 0, 14, 5, 22, 5, 18, 0, 19, 5, 14, 4, 0, 20, 15, 0, 11, 14, 15, 23, 0, 6, 15, 18, 0, 23, 8, 15, 13, 0, 20, 8, 5, 0, 2, 5, 12, 12, 0, 20, 15, 12, 12, 19, 0, 9, 20, 0, 20, 15, 12, 12, 19, 0, 6, 15, 18, 0, 20, 8, 5, 5

Problem 8.1.6. Decoding a sentence.

Decode the sequence of elements of \(\Z_{27}\)
12, 5, 20, 0, 13, 5, 0, 14, 15, 20, 0, 20, 15, 0, 20, 8, 5, 0, 13, 1, 18, 18, 9, 1, 7, 5, 0, 15, 6, 0, 20, 18, 21, 5, 0, 13, 9, 14, 4, 19, 0, 1, 4, 13, 9, 20, 0, 9, 13, 16, 5, 4, 9, 13, 5, 14, 20, 19
with the function \(C^{-1}\) from Figure 8.1.1.
Solution.
We obtain the text
 2 
from Shakespeare’s Sonnet 116, 1609
:
\begin{align*} \amp\mathtt{let-me-not-to-the-marriage-of-}\\ \amp\mathtt{true-minds-admit-impediments} \end{align*}