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

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