A Handy ASCII Table

Difficulty: Easy

Every coder needs an ASCII table. Even back in the old days, when I memorized such things as the Escape character was equal to 27 decimal, 0x1b hex, and had the keyboard shortcut ^[, I would glance at the ASCII table poster hanging on the wall to confirm that I was using the proper values in my code. And the poster looked cool.

I’m curious what happened to my old ASCII poster. It got lost in a move somewhere along my travels. Regardless, when I want to view an ASCII table today, I run a program that outputs the codes and character values, similar to what you see in Figure 1.

ASCII table

Figure 1. ASCII table output, including decimal, octal, hex, and character values.

Having the ASCII table is handy for me even today. And it can be handy for you, providing you code a program that generates output the same as shown in Figure 1. That’s this month’s Exercise:

Generate an ASCII table. The table is displayed in four columns or sticks, as shown in Figure 1. Each stick shows decimal, octal, hex, and character values. The first stick’s character values, however, are output as traditional control characters (^ means “control” in early nerd). If the actual characters were output, they would mess up the display.

Ensure that your output has a title row, as shown in Figure 1. Use pipe characters to separate the major columns.

Please try this Exercise on your own before you look at my solution.

6 thoughts on “A Handy ASCII Table

  1. I did this once and did something to output the names or descriptions of 0-32. I probably hard coded them into an array and within the loop used the array value if the current index was <= 32, otherwise just output the character.

  2. The control codes were relevant for a teletype machine, but rarely used beyond those few that still hold importance in C: \b, \t, \n, \e, and so on. Still, fun stuff.

  3. Here is my solution:

    #include <stdio.h>
    #include <ctype.h>
    char *string(char c1, char c2);
    int main(void)
        int i, j, k;
        for (i = 0; i < 4; i++)
            printf(" %3s  %3s  %3s  %2s  ", "Dec", "Oct", "Hex", "C");
        for (i = 0; i < 32; i++)
            for (j = 0; j < 4; j++)
                k = i+j*32;
                printf(" %3d  %3o  %3x  %2s ", k, k, k,
                                                                  (k < 32 ? string('^', i+2*32) : string(' ', k)));                                                                                        
                if (j < 3)
        return (0);
    char *string(char c1, char c2)
        static char s[2];
        s[0] = c1;
        s[1] = c2;
        s[2] = '\0';
        return (s);
  4. I like your solution! It doesn’t appear that you call the string() function, however.

    Use the <pre> and </pre> tags to enclose code, which preserves the plain text format. (I fixed.)

Leave a Reply