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
The number 4693 in binary is 1001001010101
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.
The number 6 in binary is 110
The binary values is 10110101.
write a c++ program to convert binary number to decimal number by using while statement
The decimal number 101 is represented by the binary number 1100101.
Every decimal number can be represented by a binary number - and conversely.
The binary equivalent of the decimal number 245 is 11110101.
decimal [ 123 ] = binary [ | | | | 0 | | ]
Decimal 11 = binary 1011
There is no decimal number for the binary number 13 because 13 cannot be a binary number.
The binary equivalent of the decimal number 131 is 10000011.
The decimal equivalent of the binary number 1111111111111111 is 65535.
The binary equivalent of the decimal number 63 is 111111.
Expressed in decimal, the binary number 111111010 represents 506.
The binary equivalent of the decimal number 14 is 00001110.