arcobi

binary to decimal converter

May 10th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, const char * argv[]) {
  7.    
  8.     cout << "Input your binary number: " << endl;
  9.     string numberSTR;
  10.     cin >> numberSTR;
  11.    
  12.     int decimalNumber = 0;
  13.     int count = 0;
  14.     for (size_t x = numberSTR.size(); x > 0; x--) {
  15.         char binary = numberSTR[count];
  16.         if (binary == '1') {
  17.             int sizeS = int(x);
  18.             decimalNumber += pow(2, sizeS-1);
  19.         }
  20.         count++;
  21.     }
  22.     cout << numberSTR << " corresponds to " << decimalNumber << " in decimal notation." << endl;
  23.  
  24.    return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment