Advertisement
noor017

Binary to Decimal

Jun 15th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. ///*********************Floating Binary to Floating Decimal Conversion*********************///
  2. ///*****************************Stop Reproduce, Start Learning*****************************///
  3. /*
  4.     **Floating Binary to Floating Decimal Conversion
  5.     **Programming Language: C
  6.     **Execute From: http://pastebin.com/edit.php?i=0zJVFx4n
  7.     **Alternate File: https://github.com/noor017/c-programming/blob/root/bin2dec.c
  8.     **Special Thanks to: Rabi Vai
  9.  
  10.     **@Start Coding From: 13 June, 2015 11:45PM
  11.     **@End of Coding: 15 June, 2015 09:30PM
  12. */
  13.  
  14. #include<stdio.h>
  15. #include<string.h>
  16. #include<conio.h>
  17.  
  18. //Start Defining power function
  19. double pow(int c, int d)
  20. {
  21.     double pow=1;
  22.     if (d >= 0)
  23.     {
  24.         int i=1;
  25.         while(i <= d)
  26.         {
  27.             pow = pow*c;
  28.             i++;
  29.         }
  30.     }
  31.     else
  32.     {
  33.         int i=0;
  34.         while (i>d)
  35.         {
  36.             pow = pow/c;
  37.             i--;
  38.         }
  39.     }
  40.     return pow;
  41. }
  42. //End Defining power function
  43.  
  44.  
  45. //Start Main function
  46. int main()
  47. {
  48.     int befDec[50], aftDec[50];
  49.     int i, j=0, k=0;
  50.     int aftDecDigits, befDecDigits, storeIntegral=0;
  51.  
  52.     double pow(int, int);
  53.     double storeFractional=0, doubleValue;
  54.  
  55.     char digits[50];
  56.     char up = 'd';
  57.  
  58.  
  59.  
  60.     //Taking binary digits in character arrary
  61.     printf("Enter binary point number: ");
  62.     scanf("%s", &digits);
  63.  
  64.     //Separating the integral and fractional part from the doubleing point value
  65.     //strlen() is an inbuilt funtion defined in string.h header file
  66.     for(i=0; i<strlen(digits); i++)
  67.     {
  68.         if(digits[i]=='.')
  69.         {
  70.             up='u';
  71.         }
  72.         else if(up=='d')
  73.         {
  74.             //ASCII value of 0 is 48, so when character is casted to integer, it results to 48, which is to be subtracted
  75.             befDec[i] = (int)digits[i]-48;
  76.             k++;
  77.         }
  78.  
  79.         else
  80.         {
  81.             aftDec[j] = (int)digits[i]-48;
  82.             j++;
  83.         }
  84.     }
  85.  
  86.     // Storing the lenght of Integral and fractional
  87.     befDecDigits = k;
  88.     aftDecDigits = j;
  89.  
  90.     //Loop to convert the integral binary part to decimal
  91.     j=0;
  92.     for(i=befDecDigits-1; i>=0; i--)
  93.     {
  94.         storeIntegral = storeIntegral + (befDec[i] *(int) pow(2,j));
  95.         j++;
  96.     }
  97.  
  98.     //Loop to convert the fractoinal binary part to doubleing point value
  99.     j = -1;
  100.     for(i = 0; i<aftDecDigits; i++)
  101.     {
  102.         storeFractional = storeFractional + (aftDec[i]*pow(2,j));
  103.         j--;
  104.     }
  105.  
  106.     //Adding both the integral and fractional part to get the resultant value
  107.     doubleValue = storeIntegral + storeFractional;
  108.     printf("doubleing point value = %0.2lf\n", doubleValue);
  109. }
  110.  
  111. //End Main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement