The following code will convert any number in any base to any other base, from binary to hexadecimal, and everything inbetween.
#include<iostream>
#include<string>
#include<sstream>
typedef unsigned long long ull;
typedef unsigned long ul;
const std::string symbols="0123456789abcdef";
std::string inputvalue(ul base)
{
using namespace std;
string value;
while(1)
{
cout<<"Enter a positive value : ";
string s;
getline(cin,s);
if(s.size())
{
for(string::iterator i=s.begin();i!=s.end();++i)
if(*i>='A' && *i<='Z')
*i+=32;
string actual = symbols.substr(0,base);
if(s.find_first_not_of(actual)!=string::npos)
{
cout<<"The value you entered is invalid for the base.\n"
<<"Please enter another value.\n"<<endl;
continue;
}
value=s;
break;
}
}
return(value);
}
ul inputbase(std::string prompt)
{
using namespace std;
ul result=0, min=2, max=16;
while(1)
{
cout<<prompt.c_str()<<" ["<<min<<".."<<max<<"] : ";
string s;
getline(cin,s);
if(s.size())
result=stoul(s,0,10);
if(result<min result>max)
cout<<"The base must be in the range "
<<min<<".."<<max<<"\n"
<<"Please enter another base.\n"<<endl;
else
break;
}
return(result);
}
ull base2dec(std::string value,ul base)
{
ull col=1, num=0;
for(std::string::reverse_iterator i=value.rbegin(); i!=value.rend(); ++i)
{
num+=symbols.find(*i,0)*col;
col*=base;
}
return(num);
}
std::string dec2base(ull dec,ul base)
{
using namespace std;
int len=1;
ull tmp=dec;
while(tmp/=base)
++len;
string value("0",len);
while(dec)
{
value[--len]=symbols[dec%base];
dec/=base;
}
return(value);
}
int main()
{
using namespace std;
ul base=inputbase("Enter the base of the value");
string value=inputvalue(base);
ul newbase=inputbase("Enter the base to convert to");
value=dec2base(base2dec(value,base),newbase);
cout<<"New value:\t"<<value.c_str()<<endl;
return(0);
}
write a c++ program to convert binary number to decimal number by using while statement
All I know is that when a number is negative, you convert the decimal into binary and if it is negative you put 1111 before the binary digits.
Write algorithms and draw a corresponding flow chart to convert a decimal number to binary equivalent?
The number 4693 in binary is 1001001010101
The number 6 in binary is 110
The binary equivalent of the decimal number 63 is 111111.
Binary 110111 is equivalent to decimal 55.
The binary number 11.1 in decimal would be 3.5
Convert 189 to binary number
13 in decimal = 1101 in binary.
110010000
It is 100011.
It is 155.
You can easily convert decimal to binary in the scientific calculator - for example, the scientific calculator found in Windows. In this case, type the number in decimal, then click on "binary" to convert to binary.
The decimal equivalent of the binary number 11101 is 29.
write a c++ program to convert binary number to decimal number by using while statement
You can use a table to convert binary to decimal & back:MSBBinary DigitLSB2827262524232221202561286432168421Figure out the greatest power that will fit into the number you want to convert to binary. Move to the next lower power of two. If you can fit into the next lower number write down a "1", if it can't put down "0". Put together the binary answer.