943,786 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3433
  • C RSS
Aug 10th, 2009
0

converting from binary to decimal and vice versa

Expand Post »
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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
revenge2 is offline Offline
79 posts
since Feb 2007
Aug 10th, 2009
0

Re: converting from binary to decimal and vice versa

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.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 10th, 2009
-1

Re: converting from binary to decimal and vice versa

  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. }
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Aug 12th, 2009
0

Re: converting from binary to decimal and vice versa

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
revenge2 is offline Offline
79 posts
since Feb 2007
Aug 12th, 2009
0

Re: converting from binary to decimal and vice versa

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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Mar 22nd, 2010
-2
Re: converting from binary to decimal and vice versa
could you pls. do me a program that converting a binary to decimal or decimal to binary with a simple codes in turbo c, which i can easily understand.. for example i will enter an number 1 then it will be automatically to convert it to its binary value. thank u! i will wait for your help! pls. help me..
Last edited by WaltP; Mar 22nd, 2010 at 4:39 am. Reason: Leaving this hijack post here because the answer is in this thread. Maybe you can figure it out if you start reading.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
akirue09 is offline Offline
1 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: sorting an array of string
Next Thread in C Forum Timeline: Searching a file





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


Follow us on Twitter


© 2011 DaniWeb® LLC