home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / utils / hex.c < prev    next >
C/C++ Source or Header  |  1999-04-14  |  950b  |  35 lines

  1. /* HEX.C translates the standard input into hexadecimal notation and sends
  2.  * the result to standard output.
  3.  * Usage: hex < foo.exe > foo.hex
  4.  * Christine M. Gianone, CUCCA, October 1986
  5.  * Modified Feb 89 to work right with Microsoft C on the PC.
  6.  */
  7.  
  8. #include <stdio.h>            /* For EOF symbol */
  9. #ifdef MSDOS
  10. #include <fcntl.h>            /* For MS-DOS O_BINARY symbol */
  11. #endif
  12.  
  13. char h[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  14. unsigned int c;
  15. int ctr = 0;
  16. char a, b;
  17.  
  18. main() {
  19. #ifdef MSDOS
  20.     setmode(fileno(stdin),O_BINARY);  /* To avoid DOS text-mode conversions */
  21. #endif
  22.     while ((c = getchar()) != EOF) {
  23.     b = c & 0xF;            /* Get low 4 bits */
  24.     a = c / 16;            /* and high 4 bits */
  25.     putchar(h[a]);            /* Hexify and output them */
  26.         putchar(h[b]);
  27.     ctr += 2;            /* Break lines every 72 characters */
  28.     if (ctr == 72) {
  29.         putchar('\n');
  30.         ctr = 0;
  31.         }
  32.     }
  33.     putchar('\n');            /* Terminate final line */
  34. }
  35.