dsdeep

Converter Binary And All

Sep 23rd, 2020 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. void alltoDecimal(int b,long long n){
  6.     long long rem;
  7. int con=0;
  8. for(int i=0;;i++){
  9.  
  10.     rem=n%10;
  11.     n=n/10;
  12.     con+=rem*pow(b,i);
  13.     if(n==0&&rem==0){
  14.         break;
  15.     }
  16. }
  17. printf("%d\n",con);
  18. }
  19. void hextoDecimal(char n[50]){
  20. long long con=0;
  21. int x=strlen(n);
  22. for(int i=0,p=x-1;i<strlen(n);i++,p--){
  23.  
  24. if(n[i]>='0'&&n[i]<='9'){
  25.     con+=(n[i]-48)*pow(16,p);
  26. }
  27. else if(n[i]>='A'&&n[i]<='Z'){
  28.   con+=(n[i]-55)*pow(16,p);
  29. }
  30.  
  31.     }
  32. printf("%lld\n",con);
  33. }
  34. void decimaltoOthers(int b,long long n){
  35. long long rem;
  36. int i,j,l;
  37. char st1[50],st2[50]={"0"};
  38. char num[10]={'0','1','2','3','4','5','6','7','8','9'};
  39. for(i=0;;i++){
  40.     rem=n%b;
  41.     n=n/b;
  42.     if(n==0&&rem==0){
  43.         break;
  44.     }
  45.     for(j=0;j<10;j++){
  46.             if(rem<10){
  47.         if(j==rem){
  48.             st1[i]=num[j];
  49.         }
  50.         }
  51.         else{
  52.             st1[i]=(char)(rem+55);
  53.             break;
  54.         }
  55.     }
  56. }
  57. l=strlen(st1);
  58. for(i=0,j=l-1;i<l;i++,j--){
  59.     st2[j]=st1[i];
  60. }
  61. puts(st2);
  62. }
  63.  
  64. int main(){
  65.  
  66.  
  67.     int a,y;
  68.     char x[50];
  69.     scanf("%d",&a);
  70.     scanf("%s %d",x,&y);
  71.             long long z=strtol(x,NULL,10);
  72.     if(a==1){
  73.             if(y==16){
  74.                hextoDecimal(x);
  75.             }
  76.             else{
  77.                alltoDecimal(y,z);
  78.             }
  79.     }
  80.     if(a==2){
  81.         decimaltoOthers(y,z);
  82.     }
  83.  
  84. return 0;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment