hello, if anyone could help me in creating a C++ code which i could use on borland builder 6, which would convert Binary input into a Decimal output. Many thanks.
Jadu
Coverting Binary to Decimal
Page 1 of 17 Replies - 10294 Views - Last Post: 05 April 2007 - 09:16 AM
Replies To: Coverting Binary to Decimal
#2
Re: Coverting Binary to Decimal
Posted 27 March 2007 - 05:53 PM
wouldn't it just be a for loop, and then break down the binary number and when it is a 1 just add 2^i to a sum variable?
#3
Re: Coverting Binary to Decimal
Posted 27 March 2007 - 06:04 PM
Maybe I'm missing something, but if all you want is a textual representation (in C):
int v; . . . // Put the 'binary' value into v. . . . char cBuffer[16]; sprintf(cBuffer, "%d", v);
#5
Re: Coverting Binary to Decimal
Posted 28 March 2007 - 04:40 PM
ermm, im new to programming and i have an idea of a do-while loop. so far i have got a main heading, "converting Binary to Decimal", and i set my binary input as char. is that right? then i put the following message "please enter '1' or '0' ". i tried to make a do-while loop for the process of the first input.
for example if i call my input "bin" and also have a counter originally starting from '0'.
i have the folowing equation.... bin*(2^counter)... is that correct?
BTW im using 'iostream' and borland 6
for example if i call my input "bin" and also have a counter originally starting from '0'.
i have the folowing equation.... bin*(2^counter)... is that correct?
BTW im using 'iostream' and borland 6
This post has been edited by jadu-85: 28 March 2007 - 04:42 PM
#6
Re: Coverting Binary to Decimal
Posted 28 March 2007 - 11:54 PM
if you post your code it is a little easier to see what you have done.
There a lots of methods for converting binary to decimal, its easier then converting back. The basic way to accept and input in any base is to use a variable called an accumulator (that does not have to be its name, that is just what it is in computer science), initalize it to zero, and then in a loop: multiply it by the base, add in the digit.
There are a couple of catches:
* accumulator probably has a maximum value for its datatype and you need to make sure you do not try to go past it.
* the user input is probably an ascii character so you need to convert it to a number first in C is the user input type is char and you want to get its numeric value use (inputC - '0') to convert from the ascii value to the numeric value.
There a lots of methods for converting binary to decimal, its easier then converting back. The basic way to accept and input in any base is to use a variable called an accumulator (that does not have to be its name, that is just what it is in computer science), initalize it to zero, and then in a loop: multiply it by the base, add in the digit.
accumulator := 0;
while (getinput is in the set [0...base-1])
{
accumulator = accumulator * base + input;
}
There are a couple of catches:
* accumulator probably has a maximum value for its datatype and you need to make sure you do not try to go past it.
* the user input is probably an ascii character so you need to convert it to a number first in C is the user input type is char and you want to get its numeric value use (inputC - '0') to convert from the ascii value to the numeric value.
#7
Re: Coverting Binary to Decimal
Posted 04 April 2007 - 09:08 AM
#include <iostream>
using namespace std;
void main()
{
bool bin;
int answer;
int sum;
int counter=1;
char x;
int Dec;
cout<<"Converting Binary To Decimal "<<endl<<endl;
cout<< "Please Enter A Binary Number Of '1' or '0' = ";
cin>>bin;
cout<<endl;
sum = bin * counter;
cout<< "The Decimal Equivilant Is : "<<sum<<endl<<endl;
if(bin=x);
{
//end program
cout<<endl;
system("PAUSE");
}
}
its missing lots of parts, but need allot of help. anyone?
#8
Re: Coverting Binary to Decimal
Posted 05 April 2007 - 09:16 AM
Well the code does not make a lot of sence to me. Though it seems to work.
First off I would warn you about the bool datatype. bool is really an int, so the user may enter a value other than 1 or 0.
Next there is the line if(bin=x);. This is more or less useless as it means exactly the same thing as bin=x, however x is uninitalized and so bin is whatever. The semicolon at the end of if(bin=x); ends the statement so that the next code in the brackets {} is executed no matter what.
I think what you may have meant was:
The program really does not do much since the bin is already and int, which already contains a 1 or a 0 and the line sum = bin * counter could be done with sum=bin just as easily.
You want to convert a binary input (I will assume a string of 1's and 0's) into a value. So your input line has to get a string, not a number (though you could just make a loop so the user inputs 1 bit at a time, more or less like the above program but with a loop).
lets say you did it that way, not very user friendly, but we can actually make it a little better.
This version will work, but it does not stop the user from entering all kinds of data that is not a valid entry, it also forced the user to inter the data in a rather uncomfortable format. But is DOES convert binary to decimal.
A better approch would be to only read in 1 char at a time, see if it is a '1' or a '0' and then convert it to a value. This way the user could enter "10010101" which would be better for the user/customer i.e. teacher and result in a better grade I would think.
First off I would warn you about the bool datatype. bool is really an int, so the user may enter a value other than 1 or 0.
Next there is the line if(bin=x);. This is more or less useless as it means exactly the same thing as bin=x, however x is uninitalized and so bin is whatever. The semicolon at the end of if(bin=x); ends the statement so that the next code in the brackets {} is executed no matter what.
I think what you may have meant was:
if (bin==x)
//end program
cout<<endl;
system("PAUSE");
}
Though why would you do that? x is unilitialized, and bin is supposed to be a 1 or a 0, and the program will end anyway.The program really does not do much since the bin is already and int, which already contains a 1 or a 0 and the line sum = bin * counter could be done with sum=bin just as easily.
You want to convert a binary input (I will assume a string of 1's and 0's) into a value. So your input line has to get a string, not a number (though you could just make a loop so the user inputs 1 bit at a time, more or less like the above program but with a loop).
lets say you did it that way, not very user friendly, but we can actually make it a little better.
int main()
{
int bin;
int answer;
unsigned int sum;
int counter;
char x;
int Dec;
cout<<"Converting Binary To Decimal "<<endl<<endl;
counter = 1;
sum=0;
do
{
cout<< "Please Enter A Binary Number Of '1' or '0' (-1 to exit) = ";
cin>>bin;
cout<<endl;
if (bin == 0 || bin == 1)
{
sum = sum * 2 + bin;
counter++;
} else if (bin != -1)
{
cout << "Invalid entry!" << endl;
}
} while (bin != -1 && counter < 32);
if (counter==32)
{
cout << "Sorry can only take 32 bit numbers" <<endl;
}
cout<< "The Decimal Equivilant Is : "<<sum<<endl<<endl;
/* if(bin==x);
{
//end program
cout<<endl;
system("PAUSE");
}*/
return 0;
}
This version will work, but it does not stop the user from entering all kinds of data that is not a valid entry, it also forced the user to inter the data in a rather uncomfortable format. But is DOES convert binary to decimal.
A better approch would be to only read in 1 char at a time, see if it is a '1' or a '0' and then convert it to a value. This way the user could enter "10010101" which would be better for the user/customer i.e. teacher and result in a better grade I would think.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|