#include <iostream>
#include <string>
// Converts value to any base (2 to 16) and returns the string representation
std::string convBase(unsigned long val, long base)
{
std::string digits = "0123456789abcdef";
std::string result;
if((base < 2) (base > 16)) {
result = "Error: base out of range.";
}
else
{
do
{
result = digits[val % base] + result;
val /= base;
} while(val);
}
return( result );
}
// Converts a binary string to its decimal equivalent
int bin2dec(std::string binary)
{
int decimal=0, bit=1;
for( int i=binary.size()-1; i>=0; --i )
{
if( binary.at(i)=='1')
decimal += bit;
bit <<= 1;
}
return( decimal );
}
int main()
{
for( int num=0; num<256; ++num )
{
std::string binary = convBase( num, 2 );
while( !binary.size() binary.size()%8 )
binary.insert( 0, "0" );
int decimal = bin2dec( binary );
std::cout << binary << " = " << decimal << std::endl;
}
return(0);
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char x, y, z;
int decimal;
cout << endl;
cout << "This program converts a 3 bit binary number to decimal.";
cout << endl;
cout << "Please enter a 3 bit binary number: ";
cin >> x >> y >> z;
decimal =(((x-48)*4) + ((y-48)*2) + ((z-48)*1));
cout << x << y << z << " binary equals "
<< decimal << " in decimal.";
cout << endl;
return 0;
}
Binary to decimal is an automatic conversion in C so there is no need to code specifically for it. This is because all data is in binary format to begin with (it's the only language the computer actually understands), but all numerical data is converted to decimal whenever data is output to the user. Numeric data can also be automatically formatted in hexadecimal without any additional code.
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);
}
Not to be pedantic, but Gray codes are binary encoded to begin with. I know what you meant but, for the benefit of others, what you really mean is how do we convert a Gray code to its decimal equivalent.
By way of an example, the Gray value 1001 is known to be 14 decimal, but 14 decimal is actually 1110 in standard binary notation. Thus in order to know that Gray 1001 really is 14, we need to know how to convert 1001 to 1110, and therefore how to convert Gray to binary. If we can do that then we can easily determine the decimal value for any given Gray.
The algorithm is quite straightforward. Start with the most-significant bit (the MSB) of the Gray code and simply copy it (the MSB is always the same for both Gray and decimal). Then XOR this bit with the next bit in the Gray code to get the next bit in the decimal code, which you then XOR with the next bit in the Gray code to get the next bit in the decimal code, and so on until you reach the least-significant bit (the LSB). It really is that simple.
The following program is written in C++, however the GrayToDecimal() function is the one that does the conversion and can be easily converted to C language. The function accepts an unsigned integer (the Gray code) and the length (the number of bits you wish to decode). The return value is an unsigned int representing the decimal value of the decoded Gray. The next two functions convert binary strings to unsigned integers and back again and are used by the main function, which allows you to enter Gray codes as strings which are then converted to both decimal and standard binary notation.
#include <iostream>
#include <string>
typedef unsigned long UINT;
const std::basic_string <char>::size_type MaxBits = sizeof( UINT ) * 8;
// converts a Gray integer of len bits to a decimal integer.
UINT GrayToDecimal( UINT Gray, const std::basic_string <char>::size_type len )
{
UINT Binary = 0;
if( len && len <= MaxBits )
{
// mask the MSB.
UINT GrayBit = 1 << ( len - 1 );
// copy the MSB.
Binary = Gray & GrayBit;
// store the bit we just set.
UINT BinBit = Binary;
// traverse remaining Gray bits.
while( GrayBit >>= 1 )
{
// shift the current binary bit
// to align with the Gray bit.
BinBit >>= 1;
// XOR the two bits.
Binary |= BinBit ^ ( Gray & GrayBit );
// store the current binary bit
BinBit = Binary & GrayBit;
}
}
return( Binary );
}
// converts a binary string to an integer
UINT BinStrToInt( std::string BinStr )
{
UINT result = 0;
std::basic_string <char>::size_type len = BinStr.length();
if( len && len <= MaxBits )
{
UINT bit = 1 << ( len - 1 );
std::basic_string <char>::size_type idx = 0;
do
if( BinStr.at( idx++ ) == '1' )
result |= bit;
while( bit >>= 1 );
}
return( result );
}
// converts an integer of len bits to a binary string.
std::string IntToBinStr( UINT bin, const std::basic_string <char>::size_type len )
{
std::string BinStr;
if( len && len <= MaxBits )
{
UINT binbit = ( 1 << ( len - 1 ));
while( binbit )
{
BinStr.append( binbit & bin ? "1" : "0" );
binbit >>= 1;
}
}
return( BinStr );
}
int main()
{
using namespace std;
string GrayCode;
do
{
GrayCode = "";
while( !GrayCode.length() )
{
cout << "Press enter to quit." << endl;
cout << "Enter a Gray code: ";
getline( cin, GrayCode, '\n');
basic_string <char>::size_type len = GrayCode.length();
if( !len )
break;
if( -1 != GrayCode.find_first_not_of( "01" ))
cout << "I'm sorry, but that is not a valid Gray code. Please use '0' and '1' characters only.\n" << endl;
else if( len > MaxBits )
cout << "I'm sorry, but I'm only programmed to convert " << MaxBits << "-bit Gray values.\n" << endl;
else
{
UINT Gray = BinStrToInt( GrayCode );
UINT Dec = GrayToDecimal( Gray, len );
string Bin = IntToBinStr( Dec, len );
cout << "\nGray code " << GrayCode << " is decimal " << Dec << " which is binary " << Bin << ".\n" << endl;
}
GrayCode = "";
}
} while( GrayCode.length() );
cout << "Quitting...\n" << 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?
A = 1010 b = 1011 c = 1100
C# EXAMPLEString text="My sample data";System.Text.ASCIIEncoding encode=new System.Text.ASCIIEncoding();//convert to binary and store in a byte[]byte[] binaryArray=encode.GetBytes(text);
write a c++ program to convert binary number to decimal number by using while statement
sprintf (to, "%d", value)
Binary 110111 is equivalent to decimal 55.
4F7B: Binary = 100111101111011 Decimal = 20347
The binary number 11.1 in decimal would be 3.5
Convert 189 to binary number
000010 in binary is 2 in decimal.
11.25 is not a valid binary.
The binary equivalent of the decimal number 63 is 111111.
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.
a) 6401 in Binary is 1100100000001b) 1010110 in decimal is 86