ASCII Programming Tricks, Round I

The clever organization of ASCII character codes lends itself to some useful programming tricks. One of those tricks involves converting from character codes ‘0’ through ‘9’ to integer values zero through nine.

From your standard, ASCII chart, behold the character codes for ‘0’ through ‘9’:

Decimal Hexadecimal Character
48 0x30 0
49 0x31 1
50 0x32 2
51 0x33 3
52 0x34 4
53 0x35 5
54 0x36 6
55 0x37 7
56 0x38 8
57 0x39 9

Ignore the Decimal column!

Pay heed to the Hexadecimal column. There you see values 0x30 through 0x39, which neatly parallel the characters ‘0’ through ‘9’. In fact, subtract 0x30 from each character code and you get the corresponding value. I think that’s pretty nifty.

Why is it nifty? Because you can convert from a character code to a value by using simple math in your code.

Yes, I know: The C library features functions that quickly convert text numbers to int values. Those functions don’t help you appreciate the ASCII codes. On the other hand, the following source code might help you understand how to use the ASCII code values as a handy char-to-int shortcut:

#include <stdio.h>

int main()
{
    int num;

    for(num=0x30;num<=0x39;num++)
        printf("0x%02X is ASCII '%c' or value %d.\n",
                num,
                num,
                num-0x30);

    return(0);
}

The for loop at Line 7 churns through hexadecimal values 0x30 through 0x39, displaying their ASCII code characters, but also the values associated with those characters. The num-0x30 calculation makes that happen, which could also be expressed as num-'0'.

Here’s the output:

0x30 is ASCII '0' or value 0.
0x31 is ASCII '1' or value 1.
0x32 is ASCII '2' or value 2.
0x33 is ASCII '3' or value 3.
0x34 is ASCII '4' or value 4.
0x35 is ASCII '5' or value 5.
0x36 is ASCII '6' or value 6.
0x37 is ASCII '7' or value 7.
0x38 is ASCII '8' or value 8.
0x39 is ASCII '9' or value 9.

Binary logic can instead be used to convert ASCII numeric code values to their true values. That equation would be num & 0x0F: The AND mask converts the ASCII codes to their literal values. The expression num - 0x30, however, is far more readable.

Obviously, this trick won’t help you with numeric strings longer than a single digit. For that you need to do more math, multiplying successive ASCII code values by 10 — but that’s a thorny problem for another day.