home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskermit / msbhex.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  3KB  |  97 lines

  1. The C programs hex.c and unhex.c translate between 8-bit binary files
  2. and straight hex files, in which every pair of hexadecimal digits corresponds
  3. to a single 8-bit byte.  Certain files in the Kermit distribution are in
  4. this format.  You can recognize them because they contain only the characters
  5. 0-9 and A-F -- no colons or other characters.
  6.  
  7. ---------------
  8.  
  9. /* HEX.C translates the standard input into hexadecimal notation and sends
  10.  * the result to standard output.
  11.  * Usage: hex < foo.exe > foo.hex
  12.  * Christine M. Gianone, CUCCA, October 1986
  13.  * Modified Feb 89 to work right with Microsoft C on the PC.
  14.  */
  15.  
  16. #include <stdio.h>            /* For EOF symbol */
  17. #ifdef MSDOS
  18. #include <fcntl.h>            /* For MS-DOS O_BINARY symbol */
  19. #endif
  20.  
  21. char h[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  22. unsigned int c;
  23. int ctr = 0;
  24. char a, b;
  25.  
  26. main() {
  27. #ifdef MSDOS
  28.     setmode(fileno(stdin),O_BINARY);  /* To avoid DOS text-mode conversions */
  29. #endif
  30.     while ((c = getchar()) != EOF) {
  31.     b = c & 0xF;            /* Get low 4 bits */
  32.     a = c / 16;            /* and high 4 bits */
  33.     putchar(h[a]);            /* Hexify and output them */
  34.         putchar(h[b]);
  35.     ctr += 2;            /* Break lines every 72 characters */
  36.     if (ctr == 72) {
  37.         putchar('\n');
  38.         ctr = 0;
  39.         }
  40.     }
  41.     putchar('\n');            /* Terminate final line */
  42. }
  43.  
  44. ---------------
  45.  
  46. /*  UNHEX.C - Program to translate a hex file from standard input
  47.  *  into an 8-bit binary file on standard output.
  48.  *  Usage: unhex < foo.hex > foo.exe
  49.  *  Christine M. Gianone, CUCCA, October 1986.
  50.  *  Modified Aug 89 to work right with Microsoft C on the PC.
  51.  */
  52.  
  53. #include <stdio.h>            /* Include this for EOF symbol */
  54. #ifdef MSDOS
  55. #include <fcntl.h>            /* For MS-DOS setmode() symbol */
  56. #endif
  57.  
  58. unsigned char a, b;            /* High and low hex nibbles */
  59. unsigned int c;                /* Character to translate them into */
  60. unsigned char decode();            /* Function to decode them  */
  61.  
  62. /* Main program reads each hex digit pair and outputs the 8-bit byte. */
  63.  
  64. main() {
  65. #ifdef MSDOS
  66.     setmode(fileno(stdout),O_BINARY); /* To avoid DOS text-mode conversions */
  67. #endif
  68.     while ((c = getchar()) != EOF) {    /* Read first hex digit */
  69.     a = c;                /* Convert to character */
  70.         if (a == '\n' || a == '\r') {    /* Ignore line terminators */
  71.             continue;
  72.     }
  73.     if ((c = getchar()) == EOF) {    /* Read second hex digit */
  74.         fprintf(stderr,"File ends prematurely\n");
  75.         exit(1);
  76.     }
  77.     b = c;                /* Convert to character */
  78.     putchar( ((decode(a) * 16) & 0xF0) + (decode(b) & 0xF) );
  79.     }
  80.     exit(0);                /* Done */
  81. }
  82.  
  83. unsigned char
  84. decode(x) char x; {              /* Function to decode a hex character */
  85.     if (x >= '0' && x <= '9')         /* 0-9 is offset by hex 30 */
  86.       return (x - 0x30);
  87.     else if (x >= 'A' && x <= 'F')    /* A-F offset by hex 37 */
  88.       return(x - 0x37);
  89.     else {                            /* Otherwise, an illegal hex digit */
  90.         fprintf(stderr,"\nInput is not in legal hex format\n");
  91.         exit(1);
  92.     }
  93. }
  94.  
  95. ------------------------------
  96.  
  97.