home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / convergent / ctaaaa.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  2KB  |  47 lines

  1. /*  UNHEX.C - Program to translate a hex file from standard input
  2.  *  into an 8-bit binary file on standard output.
  3.  *  Christine M. Gianone, CUCCA, October 1986.
  4.  *
  5.  *  Modified - Evan Arnerich, ITT/FSC, January 1993
  6.  *      added arguments for in/out file specs
  7.  */
  8. #include <stdio.h>                    /* Include this for EOF symbol */
  9.  
  10. char a, b;                            /* High and low hex nibbles */
  11.  
  12. /* Main program reads each hex digit pair and outputs the 8-bit byte. */
  13.  
  14. main(argc, argv) int argc; char *argv[]; {
  15.  
  16.     FILE *in_fp, *out_fp;
  17.  
  18.     if ((in_fp = fopen(argv[1], "r")) == NULL) {
  19.         printf("error opening %s\n", argv[1]);
  20.         exit(1);
  21.     }
  22.     if ((out_fp = fopen(argv[2], "w")) == NULL) {
  23.         printf("error opening %s\n", argv[2]);
  24.         exit(1);
  25.     }
  26.      while ((a = getc(in_fp)) != EOF) {  /* Read first hex digit */
  27.         if (a == '\n')                /* Ignore line terminators */
  28.           continue;
  29.         if ((b = getc(in_fp)) == EOF)   /* Read second hex digit */
  30.           break;
  31.         putc( ((decode(a) * 16) & 0xF0) + (decode(b) & 0xF), out_fp );
  32.     }
  33.     fclose(in_fp);
  34.     fclose(out_fp);
  35.     exit(0);                          /* Done */
  36. }
  37. decode(x) char x; {                   /* Function to decode a hex character */
  38.     if (x >= '0' && x <= '9')         /* 0-9 is offset by hex 30 */
  39.       return (x - 0x30);
  40.     else if (x >= 'A' && x <= 'F')    /* A-F offset by hex 37 */
  41.       return(x - 0x37);
  42.     else {                            /* Otherwise, an illegal hex digit */
  43.         fprintf(stderr,"Input is not in legal hex format\n");
  44.         exit(1);
  45.     }
  46. }
  47.