Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. short int convertIn (const char *number, char baseIn) {
  2.  
  3.   /*find length of number*/
  4.   stringl = 0;
  5.   total = 0;
  6.   for (i=0; number[i] != '\0'; i++){
  7.     stringl++;
  8.   }
  9.    
  10.   /* set the base */  
  11.   if (baseIn == 'd'){
  12.     base = 10;
  13.   }
  14.   if (baseIn == 'o'){
  15.     base = 8;
  16.   }
  17.   if (baseIn == 'x'){
  18.     base = 16;
  19.   }
  20.   if (baseIn == 'b'){
  21.     base = 2;
  22.   }
  23.  
  24.   for (i=0; number[i] != '\0'; i++){
  25.  
  26.     if (number[i] == '-'){
  27.       continue;
  28.     }
  29.     if  (number[i] == '0'){    
  30.       continue;
  31.     }
  32.  
  33.     subtotal = base;
  34.     j = (stringl-1-i);
  35.       while (j>1){
  36.     if ((32767/subtotal)<base){
  37.       printf("Input number too large to handle.");
  38.       exit(0);
  39.     }
  40.     subtotal = subtotal*base;
  41.     j--;
  42.       }
  43.       if (j==0){
  44.     subtotal = 1;
  45.       }
  46.     if (number[i] > '@'){
  47.       numeric = (number[i] - 55);
  48.     }
  49.  
  50.     if (number[i] < '@'){
  51.       numeric = (number[i] - 48);
  52.     }
  53.    
  54.     if ((32767/subtotal)<numeric){
  55.       printf("Input number too large to handle.");
  56.       exit(0);
  57.     }
  58.     subtotal = numeric*subtotal;
  59.  
  60.     if ((32767-total)<subtotal){
  61.       printf("Input number too large to handle.");
  62.       exit(0);
  63.     }
  64.     total = total + subtotal;
  65.    
  66.   }  
  67.  
  68.   if (number[0] == '-'){
  69.     total = total*(-1);
  70.   }
  71.   return total;
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement