Text to integer

February 13, 2021

In public-key cryptography, one often needs to calculate the exponentiation. For example, you have a message

and some integer
Part of the encryption process is to calculate

But wait! How can you calculate something like that if

is a string.

Bob immediately had a few ideas when he came across this problem. Bob is never short of ideas. He knows how to solve problems. You know, you first translate a problem into a simpler one.

The message

seemed too long. Bob simply used

Bob wrote:

How can you convert this into a number? Well, here's what Bob did:

Looks nice, right?

But, to calculate the value of the tower Bob still had to assign some numbers to the letters

Bob did the following:

If other letters are needed Bob will simply define mappings for those letters too.



seems huge. Perhaps Bob's tower isn't so efficient after all. No worries, though.

Bob found a solution in seconds:

There is one small problem though:

But that's not such a big deal, right? Everybody knows nobody wants to encrypt

Everybody wants to encrypt

However, it turns out that cryptographers already have some clever solutions for converting text into an integer.

Somewhat boring way to convert text to an integer

First of all, there are many character encodings that map characters to numbers. For example, UTF-8 maps character


Or, it maps


Using hexadecimal system, this would be:

In the hexadecimal system, each number occupies two places, so we can write:

The text

is finally converted to the number as follows:

If for some strange reason, somebody wants to calculate

is a totally meaningful message
it can be done just like that:

But that tends to be kind of a large number, right? No worries! This is where modular arithmetic comes into play.

The Alice and Bob newsletter

Get highlights of Alice and Bob's adventures delivered to your email box.

Made by XLAB with ❤️
Follow us
Twitter Twitter