943,969 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 16625
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 15th, 2004
0

i need code for binary to decimal (it should be written in 'C')

Expand Post »
hi ppl

i am fakhre, and i am new, not only at your web but also in 'C' language.
will any one of u will help me, i need a simple code written with while loop to convert binary to decimal.
your frnd
Fakhre.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fakhre is offline Offline
3 posts
since Dec 2004
Dec 15th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 15th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

ok thank you!

to say this
well my effort is just like this
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,c,d,e,f,g,h,i,j;
clrscr();

printf("enter four binary numbers for decimal value=");
scanf("%d",&a);
b=a/10;
c=b/10;
d=c/10;
e=d/10;
f=a%10;
g=b%10;
h=c%10;
i=d%10;

if(i>0)
i=8;
else {
i=0; }

if(h>0)
h=4;
else {
h=0; }

if (g>0)
g=2;
else {
g=0; }

if(f>0)
f=1;
else{
f=0; }
e=e;
j=i+h+g+f;
printf("%d\n",j);
printf("%d%d%d%d",i,h,g,f);
getch();
return 0;
}

this code has been rejected by my teacher. i want to make with while loop.
so now plz help me.
thank you.
Fakhre.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fakhre is offline Offline
3 posts
since Dec 2004
Dec 15th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

im not sure what constraints you have on how to code it but heres what i came up with after a few minutes of head scratching. it has a for and do-while and even stores the binary number if you need if afterwards. Ideally you would want to put the conversion code in a function taking either a char[8] or bool[8] parameter and returning the result. Technically since i have used an 8 bit binary number the result should by a byte (unsigned char)

  1. /* Binary to Decimal **********************************************************/
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. bool binary[8]; // unused, but if you need to store it, it will be in here
  8. char str[8]; // input string
  9. int result; // integer result
  10. int power = 1; // power of 2. This is needed because of (2 ^ i) problem (see below)
  11.  
  12. int main(void)
  13. {
  14. do
  15. {
  16. cout << "Please input an 8 bit binary number or type q to quit:";
  17. cin >> str;
  18.  
  19. result = 0; // reset to 0
  20. power = 1; // first power = 2 ^ 0 = 1
  21.  
  22. /* convert it */
  23. for(int i = 0; i < 8; i++)
  24. {
  25. if(str[i] == '1')
  26. {
  27. binary[i] = 1;// make binary number
  28. result += power; // modify result
  29. }
  30. power *= 2; /* id like to use 2 ^ i instead of power but it
  31.   DOESNT give 1, 2, 4, 8, 16 ect... */
  32. }
  33.  
  34. /* output */
  35. cout << "\nThe binary number " << str << " = " << result << "\n";
  36. } while (str[0] != 'q'); // do - while until "q" is typed
  37. return 0;
  38. }
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 16th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

well thank you for your support that you are giving me time, but i am sorry to say that problem is stil there, coz i need it for "borland c" and code should be written under #include<conio.h> and #include<stdio.h>.
well plz if you can help me in this case i will be very thankful of you...

Fakhre!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fakhre is offline Offline
3 posts
since Dec 2004
Dec 16th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

First, when you post some code, use code tags:
http://www.daniweb.com/techtalkforum...nouncementid=3

2nd, why not use a newer compiler?

3rd, here is the code posted before by 1o0oBhP in C++; i simply put it in a C format:
  1. #include<stdio.h>
  2.  
  3. char binary[8]; // unused, but if you need to store it, it will be in here
  4. char str[8]; // input string
  5. int result; // integer result
  6. int power = 1; // power of 2. This is needed because of (2 ^ i) problem (see below)
  7.  
  8. int main(void)
  9. {
  10. do
  11. {
  12. printf ("Please input an 8 bit binary number or type q to quit:");
  13. scanf ("%s", str);
  14.  
  15. result = 0; // reset to 0
  16. power = 1; // first power = 2 ^ 0 = 1
  17.  
  18. /* convert it */
  19. for(int i = 0; i < 8; i++)
  20. {
  21. if(str[i] == '1')
  22. {
  23. binary[i] = 1;// make binary number
  24. result += power; // modify result
  25. }
  26. power *= 2; /* id like to use 2 ^ i instead of power but it
  27.   DOESNT give 1, 2, 4, 8, 16 ect... */
  28. }
  29.  
  30. /* output */
  31. printf ("\nThe binary number %s = %d\n\n", str, result);
  32. } while (str[0] != 'q'); // do - while until "q" is typed
  33.  
  34. return 0;
  35. }
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Dec 16th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

Quote originally posted by fakhre ...
well my effort is just like this

<snip code>

this code has been rejected by my teacher. i want to make with while loop.
so now plz help me.
Wherever there is repetition, there ought to be a loop. Notice the repeated statements %10 and /10. Also notice the pattern of doubling the place value multiplier.
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int binary = 0; /* initialize the result to zero */
  6. int bitvalue = 1; /* starting place value for bits */
  7. int decimal = 1011; /* "decimal" value to "convert" */
  8. while ( decimal )
  9. {
  10. binary += decimal % 10 * bitvalue; /* get new bit and add to result */
  11. bitvalue *= 2; /* adjust base-2 place value */
  12. decimal /= 10; /* get next "decimal" */
  13. }
  14. printf("binary = %d\n", binary);
  15. return 0;
  16. }
  17.  
  18. /* my output
  19. binary = 11
  20. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 16th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

dave havent you just done DECIMAL to BINARY?? i thought we were doing BINARY to DECIMAL ???
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 16th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

Quote originally posted by 1o0oBhP ...
dave havent you just done DECIMAL to BINARY?? i thought we were doing BINARY to DECIMAL ???
It looked to me like "decimal-coded binary" to decimal from the original attempt (hence "decimal" in the comments).
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 16th, 2004
0

Re: i need code for binary to decimal (it should be written in 'C')

here`s the code:

  1. int main()
  2. {
  3. clrscr();
  4. int b,d=0,a[100];
  5. for(int z = 0;z<100;z++) //initialising
  6. {
  7. a[z] = 0;
  8. }
  9.  
  10. cout<<"enter the binary no."<<endl;
  11. cin>>b;
  12. int c = 0,p;
  13. while(b>0)
  14. {
  15. int r = b%10;
  16. b = b/10;
  17. p = pow(2,c);
  18. d = d + (r*p);
  19. a[c] = r;
  20. c++;
  21. }
  22. cout<<"the decimal equivalent is "<<d<<endl;
  23. return 0;
  24. }
Last edited by alc6379; Dec 17th, 2004 at 11:20 am.
Reputation Points: 10
Solved Threads: 1
Light Poster
varunrathi is offline Offline
41 posts
since Aug 2004

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: Color your console text
Next Thread in C Forum Timeline: Download 100% free Programming tutorials





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


Follow us on Twitter


© 2011 DaniWeb® LLC