943,967 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 1274
  • C++ RSS
0

binary to decimal convertor

by on May 24th, 2009
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. ...
C++ Code Snippet (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. unsigned int pow(unsigned int a, unsigned int n);
  7.  
  8. int main()
  9. {
  10. string binary;
  11. unsigned int decimal = 0;
  12.  
  13. cout << "Enter a binary number: ";
  14. cin >> binary;
  15.  
  16. for(int i = binary.length()-1; i >= 0; i--) {
  17. if(binary[i] > '1' || binary[i] < '0') {
  18. cout << "Invalid binary number!" << endl;
  19. return 1;
  20. } else {
  21. decimal += (binary[i] - '0') * pow(2, (binary.length()-i-1));
  22. }
  23. }
  24.  
  25. cout << "Decimal: " << decimal << endl;
  26. return 0;
  27. }
  28.  
  29. unsigned int pow(unsigned int a, unsigned int n)
  30. {
  31. unsigned int r = 1;
  32. while( n-- > 0 ) r *= a;
  33. return r;
  34. }
Comments on this Code Snippet
May 24th, 2009
0

Re: binary to decimal convertor

Nice code! But maybe the function name pow should be changed, to avoid any ambiguity caused if anybody chooses to include <cmath> with that ...
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 24th, 2009
0

Re: binary to decimal convertor

Yep, there you've got a point!
For those who are using the cmath library:
Change the name of the pow function to apow/mypow/pow2 or whatever you like
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 25th, 2009
0

Re: binary to decimal convertor

Here's how I would have done it:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. char binary[33]; // 32bit + '\0'
  6. unsigned int decimal = 0;
  7.  
  8. cout << "Enter binary:\n> ";
  9.  
  10. cin.getline( binary, 32 );
  11. unsigned int exp = 1;
  12.  
  13. for (int i = (int)strlen( binary ) - 1; i >= 0; --i) {
  14. if ( binary[i] == '1' )
  15. decimal += exp;
  16.  
  17. exp <<= 1;
  18. }
  19.  
  20. cout << "\nDecimal:\n> " << decimal;
  21.  
  22. cin.ignore();
  23. }
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
May 25th, 2009
0

Re: binary to decimal convertor

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 !!
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Message:
Previous Thread in C++ Forum Timeline: Visual Studio 2005 Icon Problem
Next Thread in C++ Forum Timeline: OOP !





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC