home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / cowen_tools.lzh / hex.c < prev    next >
Text File  |  1992-02-22  |  2KB  |  68 lines

  1.  
  2. /*
  3. This program accepts pairs of hex digits from the infile, and outputs
  4. them to outfile as ASCII characters.
  5.  
  6. Spaces and newlines are ignored. 
  7. The program terminates on any non - hex character.
  8.  
  9. The most common use would be to check the effects of sequences of control
  10. codes to a terminal.
  11.           
  12. This program is copyright (1989) by Cowen Software Ltd
  13. of 21/23 Bristol Ave, Manchester, England, GB-M19 3NU
  14. It is made available for any OS9/68K user freely,
  15. but may not be sold for profit, other than a reasonable 
  16. handling charge
  17. */
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. extern int errno;
  21.  
  22. main (argc,argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.   if ( argc == 2 && argv[1][0] == '-' )
  27.        {
  28.        if (argv[1][1] == '?' )
  29.            { fprintf (stderr,"\nSyntax: hex");
  30.              fprintf (stderr,"\nFunction: reads hex digits, outputs as ASCII.\n");
  31.            }
  32.        else fprintf (stderr,"\n Unknown option");  
  33.        _exit(1);
  34.        }
  35.   else
  36.       {
  37.       while (1) 
  38.          {
  39.          int achar;
  40.          achar = getd() * 16 + getd();
  41.          putchar(achar);
  42.          fflush(stdout);
  43.          }
  44.       }
  45. }
  46.  
  47. getd()
  48.     {
  49.     char digit;
  50.     int freply;
  51.     while (1)
  52.        {
  53.        freply = read(0,&digit,1);
  54.        if (freply == 0) 
  55.            _exit(0);
  56.        digit = toupper(digit);
  57.        if (digit >= '0' && digit <= '9')
  58.            return (digit - '0');
  59.        if (digit >= 'A' && digit <= 'F')
  60.            return (digit - 'A' + 10);
  61.        if ( !(digit == '\n' || digit == ' ') )
  62.            { fprintf(stderr,"\n");
  63.              fflush(stderr);
  64.              _exit(0);
  65.            }
  66.        }
  67.     }    
  68.