answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

void main()

{

int i,n,k,j,b[100];

clrscr();

printf("Enter a Number:");

scanf("%d",&n);

k=n;

for(i=0;i<=k;i++)

{

b[i]=n%2;

n=n/2;

if(n==0)break;

}

printf("\n\nBinary Equivalent:");

for(j=i;j>=0;j--)

printf("%d",b[j]);

getch();

}

Happy Coding...!!!

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

void dec2bin(long i)

{

char* str; char* p;

str = malloc( sizeof(long)*8*sizeof(char) );

p = str;

while( i > 0 )

{

(i & 0x1) ? (*p++='1') : (*p++='0');

i >>= 1;

}

while( p-- != str )

printf("%c",*p);

free(str);

}

I haven't tested this. Just grabbed it off the web.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#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);

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<stdio.h>

void main()

{

int n,j=0,i=1;

clrscr();

printf("\nEnter the number : ");

scanf("%d",&n);

while(n!=0)

{

j=j+((n%2)*i);

n=n/2;

i=i*10;

}

printf("%d",j);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

#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;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Here it goes

//decimal to binary

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

{

clrscr();

int dec,bin[255];

int i=0;

int r;

cin>>dec;

while(dec>0)

{

r=dec%2;

bin[i]=r;

i++;

dec=dec/2;

}

for(int j=i-1;j>=0;j--)

cout<<bin[j];

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

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);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

to convert binary number to hexadecimal we have to make group of four binary digit.

example 000001101100

step 1

group it into four binary number 0000 0110 1100

0000 means 0

0110 means 6

1100 means 12 i.e c

ans is 06c in hexadecimal of binary 00001101100

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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 );

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to Convert binary to decimal in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp