Section 12.3 Text
We describe how a sequence of several characters (often called a string) can be converted into a single number (encoding) and how we cabn recover the characters from that number (decoding). This is often the first step in many cryptographic protocols, after which the number thus obtained is then encrypted with an encryption function.
In Section 8.1 we had seen how to encode characters as numbers using the encoding function \(C:\A\to\Z_{27}\) from Figure 8.1.1. Now we combine this with the methods from Section 11.4 and Section 11.5 to encode several characters into one number.
In our first example we encode a short word as a number.
Example 12.3.1. Encode \(\mathtt{no}\) as a number.
Consider the word \(\mathtt{do}\text{.}\) We want to represent \(\mathtt{do}\) by a number in decimal representation in such a way that we can still recover the word.
The word \(\mathtt{do}\) consists of the two characters \(\mathtt{d}\) and \(\mathtt{o}\text{.}\) Using the encoding function \(C:\A\to\Z_{27}\) from Figure 8.1.1 we convert the two characters into numbers:
Now we have represented \(\mathtt{do}\) by two numbers, but our goal is to represent it by one numbers. For each character the encoding function \(C\) yields a number in \(\Z_{27}\) , that is a number from \(0\) to \(26\text{.}\) This is also the range of the digits of the base \(27\) representation of a number. Considering \(4\) and \(15\) as base \(27\) digits as a number we get
Because we know how to determine the base \(27\) digits of a number from we can recover \(4\) and \(15\) and thus the word \(\mathtt{no}\) from the number \(123\) , see Example 12.3.2 .
We now decode the number obtained in the example above.
Example 12.3.2. Decode \(123\).
We find the word encoded in the number \(123\)
First we find the base \(27\) expansion of the decimal number \(123\text{.}\) Following Algorithm 11.5.1 We get
\(123\fmod 27=15\) | and | \(123\fdiv 27=4\) |
\(4\fmod 27=4\) | and | \(4\fmod 27=0\) |
Decoding the \(4\) and \(15\) with \(C^{-1}:\Z_{27}\to\A\) from Figure 8.1.1 we get:
Thus the word encoded as \(123\) is \(\mathtt{do}\) .
In general we proceed as follows.
Strategy 12.3.1. Representation of text by a decimal number.
To compute the decimal representation of a word proceed as follows.
(a)
Encode the characters in the given text into numbers in \(\Z_{27}\) using the function \(C:\A\to\Z_{27}\) from Figure 8.1.1 .
(b)
Consider these numbers as coefficient of a base \(27\) expansion. Evaluate the base \(27\) expansion to obtain the decimal representation of the text.
We demonstrate how to compute a representation of the word \(\mathtt{wombat}\) as a number in decimal representation by following the steps given in the strategy above.
Example 12.3.3. Represent \(\mathtt{wombat}\) by a number.
We find a representation of the word \(\mathtt{wombat}\) as an integer.
(a)
The function \(C\) defined in Figure 8.1.1 encodes the letters in \(\mathtt{wombat}\) as the numbers \(23\) , \(15\) , \(13\) , \(2\) , \(1\) , \(20\) .(b)
We now consider these as the values of the digits of a base \(27\) number. We obtainSo, the word \(\mathtt{wombat}\) is represented by the decimal number \(338253860\) .
We demonstrate the strategy in the solution of Problem 12.3.4 .
Problem 12.3.4. Represent \(\mathtt{dog}\) by a number.
Compute a decimal representation of the word \(\mathtt{dog}\) .
The representation of \(\mathtt{dog}\) as a decimal number is \(3328\) .
Encoding the letters in \(\mathtt{dog}\) by the function \(C\) from Figure 8.1.1 we obtain
Considering as the values of the digits of a base 27 representation, we write out the base 27 expansion and evaluate it:
So, the decimal representation of the word \(\mathtt{dog}\) is the decimal number \(3328\) .
Now use the methods described above to compute a representation of a word as an integer.
Checkpoint 12.3.5. Convert word to number.
We want to compute a representation of the word
\(\mathtt{wide}\)
by one integer in decimal representation.
First represent the characters in the word by integers using the encoding function
\(C:\lbrace \mathtt{-},\mathtt{a},\mathtt{b},\dots,\mathtt{z}\rbrace \to \lbrace0,1,2,3,...26\rbrace\) with \(C(\mathtt{-})=0\text{,}\) \(C(\mathtt{a})=1\text{,...,}\)\(C(\mathtt{z})=26\text{.}\)
We obtain
\(C(\mathtt{w}) =\) , \(C(\mathtt{i}) =\) , \(C(\mathtt{d}) =\) , \(C(\mathtt{e}) =\) .
Then we compute the representation as one integer:
\(C(\mathtt{w}) \cdot 27^3 + C(\mathtt{i}) \cdot 27^2 + C(\mathtt{d}) \cdot 27 + C(\mathtt{e}) =\)
We can also work backwards to find the text that is encoded in a given decimal number.
Strategy 12.3.2. Conversion of decimal numbers to text.
To convert a decimal number to text proceed as follows.
(a)
Find the base 27 expansion of the number.(b)
Decode each digit of the base 27 expansion using the decoding function \(C^{-1}:\Z_{27}\to\A\) given by from Figure 8.1.1 to obtain the characters of the text.Problem 12.3.6. Find the word encoded as \(2234\).
Find the text encoded as the number \(2234\) .
The text represented by the decimal number \(2234\) is \(\mathtt{cat}\)
We start by converting the decimal number \(2234\) to a base \(27\) number with the method used by the base conversion algorithm ( Algorithm 11.5.1 ):
\(2234 \fdiv 27=82\) | \(2234 \fmod 27=20\) |
\(82\fdiv27=3\) | \(82 \fmod 27 = 1\) |
\(3\fdiv 27=0\) | \(3 \fmod 27 = 3\) |
Thus the base \(27\) expansion of \(2234\) is \(2234=3\cdot 27^2+1\cdot 27+20 \cdot 1\) . We decode the digits of the base \(27\) expansion number into letters using the function \(C^{-1}\) from Figure 8.1.1 :
So, the word encoded as the number \(2234\) is \(\mathtt{cat}\) .
In Checkpoint 12.3.7 apply these methods to recover a word that is encoded in a number.
Checkpoint 12.3.7. Convert number to word.
A word is encoded in the integer:
\(6565771\)
We find the digits of the base 27 representation
\(6565771 =\) \(\cdot 27^4+\) \(\cdot 27^3+\) \(\cdot 27^2+\) \(\cdot 27+\)
Applying the inverse
\(C^{-1}: \lbrace 0,1,2,3,\dots,26\rbrace \to \lbrace \mathtt{-},\mathtt{a},\mathtt{b},\mathtt{c},\dots,\mathtt{z}\rbrace\) with \(C^{-1}(0)=\mathtt{-}\text{,}\) \(C^{-1}(1)=\mathtt{a}\text{,}\) \(C^{-1}(2)=\mathtt{b}\text{,}\) \(\dots\text{,}\) \(C^{-1}(26)=\mathtt{z}\text{,}\)
of the encoding function \(C\) to these integers we obtain the word:

As far as I can tell, Navajo doesn't have a common word for 'zero'. do-neh-lini means 'neutral'.
https://xkcd.com/257
).We end this section with the remark that we made several (somewhat arbitrary) choices in our encoding and decoding scheme.
We used our simple character encoding function \(C\) . In the real world one would use an encoding standard such as ASCII or UTF-8. In both cases one would use \(256\) instead of \(27\) .
The order we chose for encoding and decoding the characters in a word or more general string was chosen to match the order in which we write base \(b\) expansions. The reverse order woukld also be a good choice.