binary to decimal

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 11
Reputation: egolovin is an unknown quantity at this point 
Solved Threads: 0
egolovin egolovin is offline Offline
Newbie Poster

binary to decimal

 
0
  #1
Oct 10th, 2008
can someone look at this? in theory it should work but i cant figure it out
thanks in advance
  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4. using namespace std;
  5. void binToDec(string getBinary);
  6. int main()
  7. {
  8. string getBinary;
  9. cout<<"Enter a Binary Number:"<<endl;
  10. cin>>getBinary;
  11. binToDec(getBinary);
  12.  
  13. return 0;
  14. }
  15. void binToDec(string getBinary)
  16. {
  17. int setLength,total=0;
  18. double placeholder=0;
  19. setLength=getBinary.size();
  20.  
  21. for(int i=setLength;i>0;i--)
  22. {
  23. int num=pow(2.0,placeholder);
  24. int test=getBinary[i];
  25. if (test==1)
  26. {
  27. total=total+num;
  28. }
  29.  
  30. placeholder=placeholder +1;
  31.  
  32. }
  33. cout<<total<<endl;
  34.  
  35.  
  36. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: binary to decimal

 
0
  #2
Oct 10th, 2008
I guess using string is not the good idea.
What we use to do was.... some how take the binary in the int or long.

then take a loop and extract the element one by one form the last...

I guess this is the efficient method.. some how you are wasting too much memory space using string.

  1. int dec;
  2. int bin;
  3. cin>>bin;
  4.  
  5. for(int i=0; bin!=0; bin/=10)
  6. dec+=bin%10*pow(2, i++);
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: egolovin is an unknown quantity at this point 
Solved Threads: 0
egolovin egolovin is offline Offline
Newbie Poster

Re: binary to decimal

 
0
  #3
Oct 10th, 2008
oh sorry i forgot to add i have to use string as binary this is hw for school. pow only works with double.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: binary to decimal

 
0
  #4
Oct 10th, 2008
Originally Posted by egolovin View Post
oh sorry i forgot to add i have to use string as binary this is hw for school. pow only works with double.

if you are such crazy about the string.
then you must subtract 48 from the each character of the string

as ASCII value of 1 is 49
when you subtract 49-48 = 1
then you can use it as normally
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: binary to decimal

 
0
  #5
Oct 10th, 2008
What
Originally Posted by egolovin View Post
can someone look at this? in theory it should work but i cant figure it out
thanks in advance
  1. if (test==1)
That line should be if (test == '1')
1 is different to '1' (character code 49, as Rhohitman said). Also, for powers of 2 you don't have to use pow(). Using the shift operator<< works fine for integers. So pow(2., x) can be replaced with 1<<x
Last edited by dougy83; Oct 10th, 2008 at 10:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: egolovin is an unknown quantity at this point 
Solved Threads: 0
egolovin egolovin is offline Offline
Newbie Poster

Re: binary to decimal

 
0
  #6
Oct 11th, 2008
Thanks alot :d
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC