/* UNHEX.C - Program to translate a hex file from standard input * into an 8-bit binary file on standard output. * Christine M. Gianone, CUCCA, October 1986. * * Modified - Evan Arnerich, ITT/FSC, January 1993 * added arguments for in/out file specs */ #include /* Include this for EOF symbol */ char a, b; /* High and low hex nibbles */ /* Main program reads each hex digit pair and outputs the 8-bit byte. */ main(argc, argv) int argc; char *argv[]; { FILE *in_fp, *out_fp; if ((in_fp = fopen(argv[1], "r")) == NULL) { printf("error opening %s\n", argv[1]); exit(1); } if ((out_fp = fopen(argv[2], "w")) == NULL) { printf("error opening %s\n", argv[2]); exit(1); } while ((a = getc(in_fp)) != EOF) { /* Read first hex digit */ if (a == '\n') /* Ignore line terminators */ continue; if ((b = getc(in_fp)) == EOF) /* Read second hex digit */ break; putc( ((decode(a) * 16) & 0xF0) + (decode(b) & 0xF), out_fp ); } fclose(in_fp); fclose(out_fp); exit(0); /* Done */ } decode(x) char x; { /* Function to decode a hex character */ if (x >= '0' && x <= '9') /* 0-9 is offset by hex 30 */ return (x - 0x30); else if (x >= 'A' && x <= 'F') /* A-F offset by hex 37 */ return(x - 0x37); else { /* Otherwise, an illegal hex digit */ fprintf(stderr,"Input is not in legal hex format\n"); exit(1); } }