| | |
binary to decimal
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
can someone look at this? in theory it should work but i cant figure it out
thanks in advance
thanks in advance
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<string> #include<cmath> using namespace std; void binToDec(string getBinary); int main() { string getBinary; cout<<"Enter a Binary Number:"<<endl; cin>>getBinary; binToDec(getBinary); return 0; } void binToDec(string getBinary) { int setLength,total=0; double placeholder=0; setLength=getBinary.size(); for(int i=setLength;i>0;i--) { int num=pow(2.0,placeholder); int test=getBinary[i]; if (test==1) { total=total+num; } placeholder=placeholder +1; } cout<<total<<endl; }
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.
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.
C++ Syntax (Toggle Plain Text)
int dec; int bin; cin>>bin; for(int i=0; bin!=0; bin/=10) dec+=bin%10*pow(2, i++);
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
•
•
•
•
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.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
What
That line should be
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
•
•
•
•
can someone look at this? in theory it should work but i cant figure it out
thanks in advance
CPP Syntax (Toggle Plain Text)
if (test==1)
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.
![]() |
Similar Threads
- Number System:Conversion from binary to decimal (C)
- binary to decimal in c++ code (C++)
- How do I Take in Hexidecimal input out put binary, decimal, and octal (Java)
- i need code for binary to decimal (it should be written in 'C') (C)
Other Threads in the C++ Forum
- Previous Thread: Simple manipulation help needed.
- Next Thread: RichEdit subclassing problem, can't call Default Proc.
Views: 575 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





