/* anum.c -- take stream of characters off stdin and put the corresponding stream of ascii codes in decimal to stdout according to the formula n - base Usage: numa [base] If "base" is not specified on the command line, it defaults to BASE. */ #define BASE 0 #define MAXC 255 /* max ascii code */ #define ASCLF 10 #include #include main(argc,argv) int argc; char **argv; { int base, chx, num, onum, pchx; if( (argc != 1) && (argc !=2) ) { printf(" %s: One optional argument \"base\" -- defaults to %d.\n", argv[0], BASE); exit(20); } base = BASE; if(argc==2) { base = atoi(argv[1]); if(base < 0) { printf(" %s: Argument \"base\" cannot be negative.\n", argv[0]); exit(21); } if(base > MAXC) { printf(" %s: Argument \"base\" may not exceed %d.\n", argv[0], MAXC); exit(22); } } num = 0; pchx = 0; while( (chx=getc(stdin)) != EOF ) { num = chx - base; if(num < 0) { fprintf(stderr,"\n %s: Code for -- %c (hex %x) < 0.\n",argv[0],chx,chx); exit(1); } if(pchx == 0) { printf("%d", num); } else { printf(" %d", num); } pchx = 1; } putchar('\n'); exit(0); }