Character codes in Excel MATH 139SPRING 2003

Character codes in Excel
Excel, like most computer programs, associates the letters of the alphabet with numbers via the American Standard Code for Information Exchange, or ASCII. The ASCII code for upper-case A is 65, for upper-case B is 66, and so on through the alphabet. Excel has a built-in function code() that returns the ASCII code of its argument.

We usually want upper-case A to map to 0, B to 1, C to 2, and so on. Let's call this the ALPHA code. To implement the ALPHA code in Excel, the function we want is

=code(x)-code("A")

where x is the cell address of the character whose code we want to find.

For example, if cell B2 contains the letter "W", then we can have Excel put its ALPHA code in cell B3 by entering the formula

=code(B2)-code("A")

into cell B3.

Now suppose we want to implement the inverse function: given a number n between 0 and 25, we want to find the letter whose ALPHA code is n. We'll use the Excel function char() to do this. But first we need to convert our given ALPHA code to an ASCII code by adding 65. The fancy way to add 65 is to add code("A"). (It looks awkward, but computer scientists love this kind of thing, because it ensures something called "portability.")

Suppose we have the number 16 in cell C4, and we want to put the letter whose ALPHA code is 16 in cell C3. We enter the formula

=char(C4+code("A"))

into cell C3. Excel displays the answer: Q.