Advertisement
AntonioVillanueva

Binario a decimal c++

Jun 24th, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. //Antonio VIllanueva Binario -> decimal
  2. #include <iostream>
  3. #include <cmath> //pow
  4. #include <algorithm> //  std::reverse
  5. using namespace std;
  6.  
  7. class binarioDecimal {
  8.     public:
  9.     binarioDecimal (const string & str):str(str), decimal (0){};
  10.    
  11.     void conversionDecimal(){//bin->dec
  12.         std::reverse(str.begin(), str.end());
  13.         for (int n= ( str.length() ) ; n>=0 ; n--){
  14.             if (str[n]=='1'){
  15.                 cout <<pow(2, n)<<" ==== "<< str[n]<< " , n = "<<n <<endl;
  16.                 decimal += pow(2, n);
  17.             }
  18.         }
  19.     }
  20.    
  21.     int getDecimal (){return decimal;}
  22.    
  23.     private:
  24.     string str;
  25.     int decimal;
  26. };
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.         string s="0000011111";
  31.         binarioDecimal d(s);
  32.         d.conversionDecimal();
  33.         cout << s<<" = "<<d.getDecimal()<< endl;       
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement