/* asctable.c this program does what it is supposed to do: print out the ASCII table from 000 to 128 :-) */ #include #include #include char cntlkey[32][4] = { "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", " BS", "TAB", " LF", " VT", " FF", " CR", " S0", " SI", "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", " EM", "SUB", "ESC", " FS", " GS", " RS", " US"}; int main(int argc, char *argv[]) { /* declaring variables */ register int i, j, k; int arglen, ascii; char ch; /* parse command line */ if (argc != 1 && argc != 2) { fprintf(stderr, "Usage: asctable [key/ascii]\n"); exit(1); } else if (argc == 2) { /* how long is the input? */ arglen = strlen(argv[1]); if ( arglen > 1 ) { /* more than 1 character, so this must be ASCII number */ ascii = atoi(argv[1]); /* print it! */ if ( iscntrl(ascii) ) printf("The key corresponding to ascii value %s is a control key %s\n", argv[1], cntlkey[ascii]); else printf("The key corresponding to ascii value %s is %c\n", argv[1], ascii); return(0); } else { /* only one character, this could be a key or a number */ ch = argv[1][0]; if ( isdigit(ch) ) { /* so it's a number after all.... */ printf("The key corresponding to ascii value %s is a control key %s\n", argv[1], cntlkey[atoi(argv[1])]); return(0); } else { /* so it is a key */ printf("The ascii value for key %s is %03d\n", argv[1], ch); return(0); } } } /* so we don't have any options, print out the ascii table! */ printf("\nASCII Table Generator / Lookup Tool\n"); printf("Usage: asctable [ key / ascii ]\n"); printf("For system control key, enclose use quotes\n"); printf("Author: Leiming Qian \n"); printf("-------------------------------------\n"); printf("ASCII"); for (i=0;i<10;i++) printf(" %02d", i); printf("\n"); printf("--------------------------------------------------------\n"); for (j=0;j<26;j++) { printf("%03d ", j*10); for (i=0;i<10-4*(j==25);i++) { /* check for control character */ k = j*10+i; if ( iscntrl(k) ) { if ( k < 32 ) printf(" %s", cntlkey[k]); else printf(" "); } else printf(" %c", k); } printf("\n"); } printf("--------------------------------------------------------\n"); printf("\n"); return(0); }