IVR_Developer -1 Newbie Poster

Hi from New Zealand.
I have been programming in C for the last 19 years.
My true love is OO which I studied at university,
but I have ended up in a C job (where the most annoying bugs are possible) with UNIX shell scripting and a proprietory ASM language called DT6 for a voice development environment under AIX on the IBM RISC boxes :-(

Everybody wants to acess the ascii table every now and then.
Here is my program to print out the ascci table, or the
extended ascii table, or you can enter the decimal,
hexadecimal, or octal or char that you want to find the ascii
representation for.

to compile
cc -o asciitable ./asciitable.c

stick the executable in your bin dir or somewhere else in your path

to invoke:
asciitable a to see the ascii table for chars 0->127
asciitable e to see the extended ascii table of chars
asciitable b to see both the ascii and extended ascii tables
asciitable c A to see the ascci table for char=='A'
asciitable n 65 to see the ascci table for decimal 65
asciitable x 41 to see the ascci table for hexidecimal 41
asciitable o 101 to see the ascii table for octal 101

As a sidenote: I am not happy with how I have to check for
ch==127
C would be better if I could put DEL=127 in my enum

#include <stdlib.h>
#include <stdio.h>

enum NON_PRINTABLE_CHARS {
	NUL=0, SOH, STX, ETX, EOT, ENQ, ACK, BEL,
	BS, TAB, LF, VT, FF, CR, SO, SI, DLE,
	DC1, DC2, DC3, DC4, NAK, SYN, ETB, CAN,
	EM, SUB, ESC, FS, GS, RS, US, SPC, DEL=127
};

char NonPrintableChars[][4] = {
	"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
	"BS", "TAB", "LF", "VT", "FF", "CR", "SO", "SI", "DLE",
	"DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN",
	"EM", "SUB", "ESC", "FS", "GS", "RS", "US", "SPC", "DEL"
};

int usage() {

	printf("\nusage: asciiTable [a|x|b]");
	printf("\nwhere:");
	printf("\n	a = Standard Ascii Character Set");
	printf("\n	e = Extended Ascii Character Set");
	printf("\n	b = Both Standard & Extended Ascii Character Sets");
	printf("\n	c = Ascii Code for a character");
	printf("\n	n = Ascii Code for a decimal number");
	printf("\n	x = Ascii Code for a hexidecimal number");
	printf("\n	o = Ascii Code for an octal number");
	return 0;
}

int printHeader(char* TableName) {

	int i;

	printf("\n%s\n", TableName);
	for (i=1; i<=4; i++) printf("Dec Hex Oct Ch\t");
	printf("\n");
	for (i=1; i<=4; i++) printf("--------------\t");
	printf("\n");
	return 0;
}

int printAsciiTable(char* TableName, int iMin, int iMax, int start, int end) {
	/*
iMin==should be 0
iMax==numbers of rows
start==first char in table e.g ascii space char=decimal 32, so to start the table at space, c==32
end==last ascii character to display. Should be <=240
*/

	int ch;
	int i;

	printHeader(TableName);
	for (i=iMin; i<=iMax; i++) {
		ch=i+start;
		if (ch <= end) {
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
			printf("\t");
		}

		/*ch=i+start+iMax+1;*/
		ch=i+start+iMax+1;
		if (ch <=end) {
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
			printf("\t");
		}

		ch=i+start+2*iMax+2;
		if (ch <=end) {
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
			printf("\t");
		}

		ch=i+start+3*iMax+3;
		if (ch <=end) {
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
		}

		printf("\n");
	}
	return 0;
}

unsigned int hex2dec( char *s )
{
	unsigned int v = 0;
	char *p;

	for( p = s; *p; p++ )
	{
		if( isdigit( *p ) )
			v = (v * 16) + (*p - '0');
		else
		{
			*p = tolower( *p );
			if( *p >= 'a' && *p <= 'f' )
				v = (v * 16) + 10 + (*p - 'a');
			else
			{
				printf("\nillegal hexadecimal char %c\n", *p);
				usage();
				exit(1);
			}
		}
	}

	return v;
}

unsigned int oct2dec( char *s )
{
	unsigned int v = 0;
	char *p;

	for( p = s; *p; p++ )
	{
		if( isdigit( *p ) && (*p <= '7'))
			v = (v * 8) + (*p - '0');
		else
		{
			printf("\nillegal octal char %c\n", *p);
			usage();
			exit(1);
		}
	}

	return v;
}


int main(int argc, char* argv[]) {

	char arg1;
	char arg2[20];
	int ch;
	int r;

	if ((argc < 2 ) || (argc > 3))  {
		usage();
		exit(1);
	}

	arg1=*argv[1];
	if (argc == 3)
		strcpy(arg2, argv[2]);

	if (argc == 2) {
		switch (arg1) {
		case 'a':
			printAsciiTable("Ascii Character Set", 0, 31, 0, 127);
			break;
		case 'e':
			printAsciiTable("Extended Character Set", 0, 28, 128, 240);
			break;
		case 'b':
			printAsciiTable("Ascii Character Set", 0, 31, 0, 127);
			printAsciiTable("Extended Character Set", 0, 28, 128, 240);
			break;
		default:
			usage();
			exit(1);
		}
		return 0;
	}
	else /*argc must be 3*/
	{
		switch (arg1) {
		case 'c':
			ch=toascii(arg2[0]);
			printf("\n%s %s\n", "Ascii Character", arg2);
			printf("Dec Hex Oct Ch\t");
			printf("\n");
			printf("--------------\t");
			printf("\n");
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
			printf("\n");
			break;
		case 'n':
			ch=atoi(arg2);
			printf("\n%s decimal %d\n", "Ascii Character", ch);
			printf("Dec Hex Oct Ch\t");
			printf("\n");
			printf("--------------\t");
			printf("\n");
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
			printf("\n");
			break;
		case 'x':
			ch=hex2dec(arg2);
			printf("\n%s hex %s decimal %d\n", "Ascii Character", arg2, ch);
			printf("Dec Hex Oct Ch\t");
			printf("\n");
			printf("--------------\t");
			printf("\n");
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
			printf("\n");
			break;
		case 'o':
			ch=oct2dec(arg2);
			printf("\n%s octal %s decimal %d\n", "Ascii Character", arg2, ch);
			printf("Dec Hex Oct Ch\t");
			printf("\n");
			printf("--------------\t");
			printf("\n");
			printf("%3d %2x %3o  ", ch, ch, ch);
			if (ch <=32)
				printf("%3s", NonPrintableChars[ch]);
			else
				if (ch ==127)
					printf("%3s", "DEL");
				else
					printf("%0c", ch);
			printf("\n");
			break;
		default:
			usage();
			exit(1);
		}
	}
}