944,126 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 238621
  • C RSS
0

Decimal to Binary conversion

by on Nov 19th, 2004
Convert a decimal (denary) integer to a binary string. An exercise in do ... while and while loops.
C Code Snippet (Toggle Plain Text)
  1. // Convert a decimal integer do a binary string
  2. // added a test printf() you can remove later
  3. // Turbo C modified for Pelles C vegaseat 19nov2004
  4.  
  5. #include <stdio.h>
  6.  
  7. void dec2bin(long decimal, char *binary);
  8.  
  9. int main()
  10. {
  11. long decimal;
  12. char binary[80];
  13.  
  14. printf("\n\n Enter an integer value : ");
  15. scanf("%ld",&decimal);
  16. dec2bin(decimal,binary);
  17. printf("\n The binary value of %ld is %s \n",decimal,binary);
  18.  
  19. getchar(); // trap enter
  20. getchar(); // wait
  21. return 0;
  22. }
  23.  
  24. //
  25. // accepts a decimal integer and returns a binary coded string
  26. //
  27. void dec2bin(long decimal, char *binary)
  28. {
  29. int k = 0, n = 0;
  30. int neg_flag = 0;
  31. int remain;
  32. int old_decimal; // for test
  33. char temp[80];
  34.  
  35. // take care of negative input
  36. if (decimal < 0)
  37. {
  38. decimal = -decimal;
  39. neg_flag = 1;
  40. }
  41. do
  42. {
  43. old_decimal = decimal; // for test
  44. remain = decimal % 2;
  45. // whittle down the decimal number
  46. decimal = decimal / 2;
  47. // this is a test to show the action
  48. printf("%d/2 = %d remainder = %d\n", old_decimal, decimal, remain);
  49. // converts digit 0 or 1 to character '0' or '1'
  50. temp[k++] = remain + '0';
  51. } while (decimal > 0);
  52.  
  53. if (neg_flag)
  54. temp[k++] = '-'; // add - sign
  55. else
  56. temp[k++] = ' '; // space
  57.  
  58. // reverse the spelling
  59. while (k >= 0)
  60. binary[n++] = temp[--k];
  61.  
  62. binary[n-1] = 0; // end with NULL
  63. }
Comments on this Code Snippet
Nov 21st, 2004
0

Re: Decimal to Binary conversion

good one
Junior Poster in Training
harshchandra is offline Offline
68 posts
since Nov 2004
Jan 20th, 2005
0

Re: Decimal to Binary conversion

Vielen Dank Herr harshchandra!
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 12th, 2006
0

Re: Decimal to Binary conversion

nice doc,

but some more comments about program would help us more in understanding the program.
Newbie Poster
magadum is offline Offline
1 posts
since Jun 2006
Nov 7th, 2007
0

Re: Decimal to Binary conversion

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:

  1. void dec2bin(long i)
  2. {
  3. char* str; char* p;
  4.  
  5. str = malloc( sizeof(long)*8*sizeof(char) );
  6. p = str;
  7. while( i > 0 )
  8. {
  9. /* bitwise AND operation with the last bit */
  10. (i & 0x1) ? (*p++='1') : (*p++='0');
  11. /* bit shift to the right, when there are no
  12. bits left the value is 0, so the loop ends */
  13. i >>= 1;
  14. }
  15. while( p-- != str ) /* print out the result backwards */
  16. printf("%c",*p);
  17.  
  18. free(str);
  19. }
Newbie Poster
beerdragoon is offline Offline
1 posts
since Oct 2007
May 2nd, 2008
0

Re: Decimal to Binary conversion

here is a recursion program for the same purpose its quite a bit shorter:
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void showbits(int h)
  4. {
  5. if(h==1)
  6. printf("%d",h);
  7. else
  8. {
  9. showbits(h/2);
  10. printf("%d",h%2);
  11. }
  12. }
  13. void main()
  14. {
  15. int nu;
  16. void showbits(int h);
  17. clrscr();
  18. printf("Num?");scanf("%d",&nu);
  19. printf("\nBin eq of %d is ",nu);
  20. showbits(nu);
  21. getch();
  22. }
Last edited by Nick Evan; Dec 9th, 2009 at 5:43 pm.
Newbie Poster
recrox is offline Offline
1 posts
since May 2008
Jul 2nd, 2008
0

Re: Decimal to Binary conversion

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?
Junior Poster
Q8iEnG is offline Offline
164 posts
since Jun 2008
Oct 19th, 2009
0

Re: Decimal to Binary conversion

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?
ksj
Newbie Poster
ksj is offline Offline
4 posts
since Sep 2009
Dec 2nd, 2009
0

Re: Decimal to Binary conversion

recrox,what is showbits for?
Newbie Poster
chescarleta18 is offline Offline
9 posts
since Oct 2009
Jan 13th, 2010
-3

Re: Decimal to Binary conversion

  1. main()
  2. {
  3. int i,c=0,sum=0;
  4. scanf("%d",&i);
  5. while(i>=0)
  6. {
  7. i=i%2;
  8. sum=sum+i*pow(10,c);
  9. i=i/2;
  10. c++
  11. }
  12. printf("%d",sum);
  13. }
Last edited by Nick Evan; Jan 14th, 2010 at 5:13 pm.
Newbie Poster
arun26 is offline Offline
1 posts
since Jan 2010
Message:
Previous Thread in C Forum Timeline: beginner problem in c
Next Thread in C Forum Timeline: Library Linking in Suse Linux





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC