converting from binary to decimal and vice versa

Reply

Join Date: Feb 2007
Posts: 76
Reputation: revenge2 is an unknown quantity at this point 
Solved Threads: 2
revenge2's Avatar
revenge2 revenge2 is offline Offline
Junior Poster in Training

converting from binary to decimal and vice versa

 
0
  #1
Aug 10th, 2009
hello there, I am working on a small c program that automatically detects whether the input given is a decimal number or a binary number and coverts accordingly.

what is the best way to convert a binary number to a decimal number and a decimal number to a binary number.

i found this to be the easiest when converting from binary to deicmal
http://www.wikihow.com/Convert-from-Decimal-to-Binary


but my program doesnt work..?

  1. /*decimal to binary*/
  2. #include <stdio.h>
  3. int main() {
  4. int dec;
  5. int hold=0;
  6. printf("decimal number : ");
  7. scanf("%d",&dec);
  8.  
  9. hold=dec%2;
  10.  
  11. while (hold!=1){
  12. if (hold == 0){
  13. printf("0");
  14. }else if(hold >1){
  15. printf("1");
  16. }else{
  17. printf("1");
  18. }
  19. hold=hold%2;
  20. }
  21. }
what have i done wrong?
Last edited by revenge2; Aug 10th, 2009 at 8:51 pm.
|--- A Thank you to all the Forum members ---|
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: converting from binary to decimal and vice versa

 
0
  #2
Aug 10th, 2009
You don't have any divide's in there. You can't just use modulo for the whole thing. Also, you forgot to read the number backwards.
Last edited by Hiroshe; Aug 10th, 2009 at 9:32 pm.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 66
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Re: converting from binary to decimal and vice versa

 
-1
  #3
Aug 10th, 2009
  1. /*decimal to binary*/
  2. #include <stdio.h>
  3.  
  4. void printBinary(const unsigned char val) {
  5.  
  6. for(int i = 7; i >= 0; i--)
  7.  
  8. if(val & (1 << i))
  9.  
  10. printf("1");
  11.  
  12. else
  13.  
  14. printf("0");
  15. printf("\n");
  16.  
  17. }
  18.  
  19. int main() {
  20. int dec;
  21. int hold=0;
  22. printf("Integer number : ");
  23. scanf("%d",&dec);
  24. printBinary((unsigned char)dec);
  25. }
Ambarish
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 76
Reputation: revenge2 is an unknown quantity at this point 
Solved Threads: 2
revenge2's Avatar
revenge2 revenge2 is offline Offline
Junior Poster in Training

Re: converting from binary to decimal and vice versa

 
0
  #4
Aug 12th, 2009
hmmm why doesn't this work?

  1. #include <stdio.h>
  2.  
  3. void bin_c(int x);
  4.  
  5. int main(){
  6. int x;
  7. printf("Enter a number:");
  8. scanf("%d",x);
  9.  
  10. bin_c(x);
  11. return 0;
  12. }
  13.  
  14. void bin_c(int x){
  15.  
  16. if (x == 1){
  17. printf("1");
  18. }
  19.  
  20. while (x!=1){
  21. int y;
  22. y=x/2;
  23. if (y == 0){
  24. printf("0");
  25. }
  26. if (y > 1){
  27. printf("1");
  28. }
  29. if (y == 1){
  30. printf("1");
  31. }
  32. bin_c(y);
  33. }
  34.  
  35. }

i know i have to reverse the order but this doesnt work why is this?
Last edited by revenge2; Aug 12th, 2009 at 2:01 am.
|--- A Thank you to all the Forum members ---|
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: converting from binary to decimal and vice versa

 
0
  #5
Aug 12th, 2009
To get a binary value from a decimal value
v = val %2; -- v will contain the value of the ones digit. Then, after you get the digit, remove it to get the next digit:
v = val /2; Loop until val is 0.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC