home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / kit / part01 / btoa / hexdecode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  1.2 KB  |  55 lines

  1.  
  2. /*
  3.  
  4.  #    #  ######  #    #  #####   ######   ####    ####   #####   ######
  5.  #    #  #        #  #   #    #  #       #    #  #    #  #    #  #
  6.  ######  #####     ##    #    #  #####   #       #    #  #    #  #####
  7.  #    #  #         ##    #    #  #       #       #    #  #    #  #        ###
  8.  #    #  #        #  #   #    #  #       #    #  #    #  #    #  #        ###
  9.  #    #  ######  #    #  #####   ######   ####    ####   #####   ######   ###
  10.  
  11.     Converts hex from stdin to stdout.
  12.  
  13.     R. Manfredi and E. Mogenet, June 1990.
  14. */
  15.  
  16. /*
  17.  * $Id: hexdecode.c,v 2.0 91/02/19 15:49:29 ram Exp $
  18.  *
  19.  * $Log:    hexdecode.c,v $
  20.  * Revision 2.0  91/02/19  15:49:29  ram
  21.  * Baseline for first official release.
  22.  * 
  23.  */
  24.  
  25. #include <stdio.h>
  26.  
  27. static char    rcs_id[] = "@(#)hexdecode, convert hex from stdin to stdout";
  28.  
  29. #define ishexa(x) (((x)>='0' && (x)<='9') || ((x)>='A' && (x)<='F'))
  30. #define tohexa(x) ( (x)>='A' ? (x)-'A'+0xA : (x)-'0' )
  31.  
  32. main(argc, argv)
  33. int    argc;
  34. char    **argv;
  35. {
  36.     int    c1, c2;
  37.  
  38.     while (EOF != (c1 = getchar())) {
  39.         if (ishexa(c1)) {
  40.             c2 = getchar();
  41.             if (ishexa(c2))
  42.                 putchar((tohexa(c1) << 4) | (tohexa(c2)));
  43.             else
  44.                 goto corrupted;
  45.         } else if ( c1 != '\n' ) {
  46. corrupted:
  47.             fprintf(stderr, "%s: file corrupted\n", argv[0]);
  48.             exit(1);
  49.         }
  50.     }
  51.     exit(0);
  52. }
  53.  
  54.  
  55.