Advertisement
theelitenoob

Base Convertor

Jul 13th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[]){
  4.     if(argv[1] == NULL){
  5.         printf("\nEnter an argument to the program!\n");
  6.         exit(8);
  7.     }
  8.     int in;
  9.     short i, j; //iterator
  10.     char b[12];
  11.     if(sscanf( argv[1], "%d", &in ) == 1);
  12.     else{
  13.         printf("Enter an integer for the argument!\n");
  14.         exit(8);
  15.     }
  16.     //only have to calculate binary and 32 bit, the rest is automatic!
  17.     for(i = 11; i >= 0; i--){
  18.         if((1 << i) & in)
  19.             b[11 - i] = '1';
  20.         else
  21.             b[11 - i] = '0';
  22.     }
  23.     b[12] = 0;
  24.     char b32[32]={"0123456789abcdefghijklmnopqrstuv"};
  25.     i = j = 0; // reset iterators
  26.     char b32result[32]; // the final conversion
  27.     while(in){
  28.         b32result[i] = b32[in%32];
  29.         in = in/32;
  30.         i++;
  31.     }
  32.     //notice the fact you can use %d, %x %o for decimal, hex and octal automatically.
  33.     printf("\nDecimal: %d\nBinary: %s\nOctal: %o\nHex: %x\nBase32: %s\n", in, b, in, in, b32result);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement