Cardona Designs | Setting Standards

CAT | C

May/11

7

ASCII Table in C

In an attempt to brush up on my binary/oct/hex conversions as well as better understand ASCII I decided to write an extensive ASCII Table this weekend on paper. After getting about half way through the idea appeared to write a brief program in C that could print to the console a formatted ASCII table.

Because of the power of C this ended up being much easier than I expected. This table shows the ASCII character and value as well as the decimal, octal, and hexidecimal value. There is no binary conversion in the standard io library so this table lacks a binary conversion.

#include <stdio.h>
main() {
    int i;
    for(i = 33; i <= 127; ++i) {
    printf("ascii char: %c, decimal: %3d, hexadecimal: %x, 
    octal: %3o, ascii number: %3d\n", i, i, i, i, i); 
    }
}

Of special note is the printf output formatting function. Notice in the first argument after each %. C is able to take the value of i and convert it to multiple formats that easily.

%c renders the ASCII character. %d renders the decimal value. %x renders the %hexidecimal value. Finally %o renders the octal value.

All code is licensed under Creative Commons

No tags

Theme Design by devolux.nh2.me