daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int baseCheck (char *num , int srcBas){
  7.     int i, tf=1;
  8.    
  9.     if (srcBas==2){
  10.         for(i=0;i<strlen(num)-1;i++){
  11.             if(num[i]!='0' && num[i] !='1')
  12.                 tf=-1;
  13.         }
  14.     }
  15.     else if (srcBas==16){
  16.         for (i=0;i<strlen(num)-1;i++){
  17.             if((num[i] > '9' || num[i] < 'A') && ((num[i] > 'F') || (num[i] < 'a' && num[i] > 'f'))) {
  18.                 tf=-1;
  19.             }
  20.         }
  21.     }
  22.  
  23.    
  24.  
  25. return tf;
  26. }
  27.  
  28. int decimalV(char *num , int srcBas){  //Decimal Calculation
  29.     int i ,j, tmpnum=0 ,tmp2=0;
  30.    
  31.     for(i=strlen(num)-2, j=0; i>=0;i--,j++){
  32.         if(num[i] >= 'A' || num[i]<='Z'){
  33.             tmpnum=num[i]-'A'+10;
  34.         }
  35.         else if (num[i] >='a' || num[i]<='z'){
  36.             tmpnum=num[i]-'a'+10;
  37.         }
  38.         else if( num[i] >='0' || num[i] <= '9'){
  39.             tmpnum=num[i]-'0';
  40.         }
  41.         else {
  42.             printf("Error , Invalid Number");
  43.             return 0;
  44.         }
  45.  
  46.         tmp2=tmp2+((int)pow(srcBas,j)*tmpnum);
  47.     }
  48.  
  49.  return tmp2;
  50.  
  51. }
  52.  
  53. char* destBas(int decimalnum , int destbase){ //Destination convertion
  54.     int i,tmp ,size;
  55.     char* ans;
  56.     size=((int)(log(decimalnum)/log(destbase)) +1);
  57.     ans=(char*)malloc(size*sizeof(char));
  58.     for(i=strlen(ans)-2 ; i >= 0 ; i++){
  59.         tmp=decimalnum%destbase;
  60.         if(tmp>=0 || tmp <= 9){
  61.             ans[i]=tmp+'0';
  62.         }
  63.         else if(tmp>='A'-10 || tmp <='Z'-10){
  64.             ans[i]=tmp+'A'-10;
  65.         }
  66.         else if(tmp >='a'-10 || tmp <='z'-10){
  67.             ans[i]=tmp+'a'-10;
  68.         }
  69.         decimalnum=decimalnum/destbase;
  70.     }
  71.  
  72.     return ans;
  73. }
  74. char* baseConvert(char *num, int srcBas, int destBas){
  75.     char *ans;
  76.    
  77.     if(srcBas<2 || srcBas >36){
  78.         printf("Source base must be between 2 to 36!\n");
  79.         return " ";
  80.     }
  81.     if(destBas<2 || destBas >36){
  82.         printf("Destination base must be between 2 to 36!\n");
  83.         return " ";
  84.     }
  85.  
  86.    
  87.    
  88.        
  89.  
  90.  
  91.        
  92.    
  93. return ans;
  94.  
  95. }
  96.  
  97.  
  98.  
  99. void main(){
  100.     printf("%s\n", baseConvert("aBc", 16, 10));
  101.     printf("%s\n", baseConvert("1646244", 10, 36));
  102.  
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top