1,105,214 Community Members

Decimal to Binary conversion

Member Avatar
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
 
2
 

Convert a decimal (denary) integer to a binary string. An exercise in do ... while and while loops.

// Convert a decimal integer do a binary string
// added a test printf() you can remove later
// Turbo C modified for Pelles C     vegaseat    19nov2004

#include <stdio.h>

void dec2bin(long decimal, char *binary);

int main()
{
  long decimal;
  char binary[80];

  printf("\n\n Enter an integer value : ");
  scanf("%ld",&decimal);
  dec2bin(decimal,binary);
  printf("\n The binary value of %ld is %s \n",decimal,binary);

  getchar();  // trap enter 
  getchar();  // wait
  return 0;
}

//
// accepts a decimal integer and returns a binary coded string
//
void dec2bin(long decimal, char *binary)
{
  int  k = 0, n = 0;
  int  neg_flag = 0;
  int  remain;
  int  old_decimal;  // for test
  char temp[80];

  // take care of negative input
  if (decimal < 0)
  {      
    decimal = -decimal;
    neg_flag = 1;
  }
  do 
  {
    old_decimal = decimal;   // for test
    remain    = decimal % 2;
    // whittle down the decimal number
    decimal   = decimal / 2;
    // this is a test to show the action
    printf("%d/2 = %d  remainder = %d\n", old_decimal, decimal, remain);
    // converts digit 0 or 1 to character '0' or '1'
    temp[k++] = remain + '0';
  } while (decimal > 0);

  if (neg_flag)
    temp[k++] = '-';       // add - sign
  else
    temp[k++] = ' ';       // space

  // reverse the spelling
  while (k >= 0)
    binary[n++] = temp[--k];

  binary[n-1] = 0;         // end with NULL
}
Member Avatar
harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: -3 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

good one

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Vielen Dank Herr harshchandra!

Member Avatar
magadum
Newbie Poster
1 post since Jun 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

nice doc,

but some more comments about program would help us more in understanding the program.;)

Member Avatar
beerdragoon
Newbie Poster
1 post since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I love the code samples on the site, I've been a long time reader and never posted anything so I figured I should contribute something (even if it sucks).

Anyhow people who teach programming like to use this exercise to teach the basics of loops, arrays and input parsing. As for the actual binary conversion, you can also do it with bit shifting but it tends to make for messy code. Here's a quick example:

void dec2bin(long i)
{
	char* str; char* p;

	str = malloc( sizeof(long)*8*sizeof(char) );
	p = str;
	while( i > 0 )
	{
		/* bitwise AND operation with the last bit */
		(i & 0x1) ? (*p++='1') : (*p++='0'); 
		/* bit shift to the right, when there are no
		bits left the value is 0, so the loop ends */
		i >>= 1; 
	}
	while( p-- != str ) /* print out the result backwards */
		 printf("%c",*p);

	free(str);
}
Member Avatar
recrox
Newbie Poster
1 post since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

here is a recursion program for the same purpose its quite a bit shorter:

#include<stdio.h>
#include<conio.h>
void showbits(int h)
{
if(h==1)
printf("%d",h);
else
{
showbits(h/2);
printf("%d",h%2);
}
}
void main()
{
int nu;
void showbits(int h);
clrscr();
printf("Num?");scanf("%d",&nu);
printf("\nBin eq of %d is ",nu);
showbits(nu);
getch();
}
Member Avatar
Q8iEnG
Junior Poster
189 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Really nice Idea I was searching for the formula to change from digit to binary,


but please guys could you type some comments in the codes to understand them?

Member Avatar
ksj
Newbie Poster
4 posts since Sep 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry if this is a dumb question, but why use char string and not integer array to store the binary number?
And if i want to use the obtained binary number for further calculations, how is it possible with char string?

Member Avatar
chescarleta18
Newbie Poster
9 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

recrox,what is showbits for?

Member Avatar
arun26
Newbie Poster
1 post since Jan 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-3
 
main()
{
int i,c=0,sum=0;
scanf("%d",&i);
while(i>=0)
{
i=i%2;
sum=sum+i*pow(10,c);
i=i/2;
c++
}
printf("%d",sum);
}
You
Post:
Start New Discussion
View similar articles that have also been tagged: