home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 14 / cnvrtc.c < prev    next >
C/C++ Source or Header  |  1992-01-14  |  2KB  |  111 lines

  1. // cnvrtc.c RHS 12/25/91
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #include<ctype.h>
  7.  
  8. char *binarystrs[8] = { "000", "001", "010", "011", 
  9.                         "100", "101", "110", "111" };
  10.  
  11. char *oct2bin(char *octstr, char *binstr)
  12.     {
  13.     char *temp = binstr;
  14.  
  15.     for( *binstr = ' '; *octstr; octstr++)
  16.         {
  17.         strcpy(binstr,binarystrs[(*octstr)-'0']);
  18.         binstr += 3;
  19.         }
  20.     for(binstr = temp; *binstr == '0'; binstr++);
  21.     strcpy(temp,binstr);
  22.     return temp;
  23.     }
  24.  
  25. long bin2dec(char *binstr)
  26.     {
  27.     long result = 0L;
  28.  
  29.     for( ; *binstr; binstr++)
  30.         result = (result*2) + (*binstr-'0');
  31.     return result;
  32.     }
  33.  
  34. typedef struct _convert
  35.     {
  36.     char type;
  37.     char *fmtstr;
  38.     } Convert;
  39.  
  40. Convert CnvtTable[3] = 
  41.     {
  42.     {   'o',"%lo",   },
  43.     {   'd',"%lu",   },
  44.     {   'h',"%lx",   }
  45.     };
  46.  
  47. void main(int argc, char **argv)
  48.     {
  49.     char *number = argv[1];
  50.     char radix = '\0';
  51.     long result;
  52.     int i;
  53.     char octstr[15], binstr[40];
  54.  
  55.     if(argc != 2)
  56.         {
  57. usage:
  58.         printf( "Usage: CONVERT <n>[r]\n"
  59.                 "\twhere n >= 0 and n >= 4294967295 and 'r' is the radix (b,o,h,d)\n"
  60.                 "\tand 'r' is the radix (b,o,h,d)\n"
  61.                 "\texamples: 10b Ah 3d 4o (binary 10, A hex, 3 decimal, 4 binary\n"
  62.                 "\t(numbers without a radix are assumed to be decimal)\n");
  63.         exit(0);
  64.         }
  65.  
  66.     radix = '\0';
  67.     radix = number[strlen(number)-1];
  68.  
  69.     if(!isalpha(radix))
  70.         radix = 'd';
  71.     else
  72.         number[strlen(number)-1] = '\0';
  73.  
  74.     switch(tolower(radix))
  75.         {
  76.         case 'b':
  77.             result = bin2dec(number);
  78.             break;
  79.  
  80.         case 'o':
  81.         case 'h':
  82.         case 'd':
  83.             for(i = 0; i < 3; i++)
  84.                 if(radix == CnvtTable[i].type)
  85.                     {
  86.                     sscanf(number,CnvtTable[i].fmtstr,&result);
  87.                     break;
  88.                     }
  89.             break;
  90.  
  91.         default:
  92.             goto usage;
  93.         }
  94.  
  95.     sprintf(octstr,"%lo",result);
  96.     printf("%sb = ",oct2bin(octstr,binstr));
  97.     printf("%loo = %0lud = %lxh\n",result,result,result);
  98.     }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.