Decimal to Binary conversion

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Nov 19th, 2004, 12:01 pm |
1
Convert a decimal (denary) integer to a binary string. An exercise in do ... while and while loops.
Quick reply to this message  
C Syntax
  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. }
0
harshchandra harshchandra is offline Offline | Nov 21st, 2004
good one
 
0
vegaseat vegaseat is offline Offline | Jan 20th, 2005
Vielen Dank Herr harshchandra!
 
0
magadum magadum is offline Offline | Jun 12th, 2006
nice doc,

but some more comments about program would help us more in understanding the program.
 
0
beerdragoon beerdragoon is offline Offline | Nov 7th, 2007
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. }
 
0
recrox recrox is offline Offline | May 2nd, 2008
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 niek_e; 15 Days Ago at 5:43 pm.
 
0
Q8iEnG Q8iEnG is offline Offline | Jul 2nd, 2008
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?
 
0
ksj ksj is offline Offline | Oct 19th, 2009
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?
 
0
chescarleta18 chescarleta18 is offline Offline | 22 Days Ago
recrox,what is showbits for?
 
 

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC