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

Reply

Join Date: Dec 2004
Posts: 3
Reputation: fakhre is an unknown quantity at this point 
Solved Threads: 0
fakhre fakhre is offline Offline
Newbie Poster

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

 
0
  #1
Dec 15th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,345
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #2
Dec 15th, 2004
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 3
Reputation: fakhre is an unknown quantity at this point 
Solved Threads: 0
fakhre fakhre is offline Offline
Newbie Poster

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

 
0
  #3
Dec 15th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

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

 
0
  #4
Dec 15th, 2004
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. }
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 3
Reputation: fakhre is an unknown quantity at this point 
Solved Threads: 0
fakhre fakhre is offline Offline
Newbie Poster

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

 
0
  #5
Dec 16th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

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

 
0
  #6
Dec 16th, 2004
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,345
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #7
Dec 16th, 2004
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. */
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

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

 
0
  #8
Dec 16th, 2004
dave havent you just done DECIMAL to BINARY?? i thought we were doing BINARY to DECIMAL ???
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,345
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #9
Dec 16th, 2004
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).
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 41
Reputation: varunrathi is an unknown quantity at this point 
Solved Threads: 1
varunrathi's Avatar
varunrathi varunrathi is offline Offline
Light Poster

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

 
0
  #10
Dec 16th, 2004
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.
"Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC