| | |
binary to decimal convertor
Please support our C++ advertiser: Intel Parallel Studio Home
This snippet allows you to convert a text representation(*) of a binary number to a decimal number 
(*): With 'text representation' I mean for example a string containing "1001" or "10001110011", etc. ...

(*): With 'text representation' I mean for example a string containing "1001" or "10001110011", etc. ...
#include <iostream> #include <string> using namespace std; unsigned int pow(unsigned int a, unsigned int n); int main() { string binary; unsigned int decimal = 0; cout << "Enter a binary number: "; cin >> binary; for(int i = binary.length()-1; i >= 0; i--) { if(binary[i] > '1' || binary[i] < '0') { cout << "Invalid binary number!" << endl; return 1; } else { decimal += (binary[i] - '0') * pow(2, (binary.length()-i-1)); } } cout << "Decimal: " << decimal << endl; return 0; } unsigned int pow(unsigned int a, unsigned int n) { unsigned int r = 1; while( n-- > 0 ) r *= a; return r; }
0
•
•
•
•
Nice code! But maybe the function name
...
pow should be changed, to avoid any ambiguity caused if anybody chooses to include <cmath> with that
... 0
•
•
•
•
Yep, there you've got a point!
For those who are using the
Change the name of the
For those who are using the
cmath library:Change the name of the
pow function to apow/mypow/pow2 or whatever you like 
0
•
•
•
•
Here's how I would have done it:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char binary[33]; // 32bit + '\0' unsigned int decimal = 0; cout << "Enter binary:\n> "; cin.getline( binary, 32 ); unsigned int exp = 1; for (int i = (int)strlen( binary ) - 1; i >= 0; --i) { if ( binary[i] == '1' ) decimal += exp; exp <<= 1; } cout << "\nDecimal:\n> " << decimal; cin.ignore(); }
0
•
•
•
•
I'm glad to know that there was actually no need for a separate function like pow, the bit shifting is a superior method to multiplicate it every time by two!
And William, your program will threat any other value than '1' as a '0', this isn't wrong IMO, but I only wanted to let you know in case you didn't (but probably you did know this already, because you're always one step ahead
)
I learned again from your code, you're superior in writing efficient and very easy to understand code
!!
And William, your program will threat any other value than '1' as a '0', this isn't wrong IMO, but I only wanted to let you know in case you didn't (but probably you did know this already, because you're always one step ahead
)I learned again from your code, you're superior in writing efficient and very easy to understand code
!! Similar Threads
- binary to decimal (C++)
- how to convert Binary numbers to decimal numbers and decimal numbers to binary (C++)
- Convert Binary to Decimal and from Decimal to Binary (C++)
- binary to decimal (C)
- binary to decimal (C)
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets



