home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1546 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: djm@eng.umd.edu (David J. MacKenzie)
  2. Newsgroups: alt.sources
  3. Subject: conv.c -- simple numeric base converter
  4. Message-ID: <DJM.90Jul2170212@twiddle.eng.umd.edu>
  5. Date: 2 Jul 90 21:02:12 GMT
  6.  
  7.  
  8. /* conv - convert numbers between bases
  9.  
  10.    Usage: conv [-dho] number...
  11.  
  12.    Print the value of each given NUMBER in the output base selected
  13.    by the options.
  14.  
  15.    Options:
  16.    -d        Output in decimal.
  17.    -h        Output in hexadecimal.
  18.    -o        Output in octal.
  19.  
  20.    Non-option arguments are taken to be numbers in one of the three
  21.    bases.  Hex numbers start with 0x; octal numbers start with 0; all
  22.    other arguments are assumed to be decimal numbers.
  23.  
  24.    David MacKenzie <djm@eng.umd.edu>
  25.    Public domain.
  26.    Latest revision: 07/02/90 */
  27.  
  28. #include <stdio.h>
  29.  
  30. long convert ();
  31. void invalid_num ();
  32. void output ();
  33.  
  34. /* The name this program was run with. */
  35. char *program_name;
  36.  
  37. void
  38. main (argc, argv)
  39.      int argc;
  40.      char **argv;
  41. {
  42.   extern int optind;
  43.   int optc;
  44.   int output_base;
  45.  
  46.   program_name = argv[0];
  47.   output_base = 0;
  48.  
  49.   while ((optc = getopt (argc, argv, "dho")) != EOF)
  50.     {
  51.       switch (optc)
  52.     {
  53.     case 'd':
  54.       output_base = 10;
  55.       break;
  56.     case 'h':
  57.       output_base = 16;
  58.       break;
  59.     case 'o':
  60.       output_base = 8;
  61.       break;
  62.     default:
  63.       output_base = 0;
  64.       goto usage;
  65.     }
  66.     }
  67.  
  68.  usage:
  69.   if (optind == argc || output_base == 0)
  70.     {
  71.       fprintf (stderr, "\
  72. Usage: %s [-dho] [0xhexnum] [0octnum] [decnum] ...\n", argv[0]);
  73.       exit (1);
  74.     }
  75.  
  76.   for (; optind < argc; ++optind)
  77.     output (convert (argv[optind]), output_base);
  78.  
  79.   putchar ('\n');
  80.  
  81.   exit (0);
  82. }
  83.  
  84. long 
  85. convert (anumber)
  86.      char *anumber;
  87. {
  88.   long number;
  89.  
  90.   if (anumber[0] == '0' && anumber[1] == 'x')
  91.     {
  92.       /* Convert from hex; sscanf returns number of conversions performed. */
  93.       if (anumber[2] == '\0' || sscanf (anumber + 2, "%lx", &number) == 0)
  94.     invalid_num (anumber);
  95.     }
  96.   else if (anumber[0] == '0')
  97.     {
  98.       /* Convert from octal. */
  99.       if (anumber[1] == '\0')
  100.     number = 0L;
  101.       else if (sscanf (anumber + 1, "%lo", &number) == 0)
  102.     invalid_num (anumber);
  103.     }
  104.   else
  105.     {
  106.       /* Convert from decimal. */
  107.       if (anumber[0] == '\0' || sscanf (anumber, "%ld", &number) == 0)
  108.     invalid_num (anumber);
  109.     }
  110.   return number;
  111. }
  112.  
  113. void 
  114. output (number, base)
  115.      long number;
  116.      int base;
  117. {
  118.   switch (base)
  119.     {
  120.     case 10:
  121.       printf ("%ld ", number);
  122.       break;
  123.     case 8:
  124.       printf ("0%lo ", number);
  125.       break;
  126.     case 16:
  127.       printf ("0x%lx ", number);
  128.       break;
  129.     }
  130. }
  131.  
  132. void 
  133. invalid_num (anumber)
  134.      char *anumber;
  135. {
  136.   fprintf (stderr, "%s: %s: invalid number\n", program_name, anumber);
  137.   exit (1);
  138. }
  139. --
  140. David J. MacKenzie <djm@eng.umd.edu> <djm@ai.mit.edu>
  141.