954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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.

fakhre
Newbie Poster
3 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

ok thank you!

to say this ;)
well my effort is just like this
#include
#include
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.

fakhre
Newbie Poster
3 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

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) :)

/* Binary to Decimal **********************************************************/

#include <iostream>
#include <cstdio>
using namespace std;

bool binary[8]; // unused, but if you need to store it, it will be in here
char str[8];    // input string
int result;     // integer result
int power = 1;  // power of 2. This is needed because of (2 ^ i) problem (see below)

int main(void)
{
    do  
    {
        cout << "Please input an 8 bit binary number or type q to quit:";
        cin >> str;
    
        result = 0; // reset to 0
        power = 1;  // first power = 2 ^ 0 = 1
        
        /* convert it */
        for(int i = 0; i < 8; i++)
        {
            if(str[i] == '1')
            {
                binary[i] = 1;// make binary number
                result += power; // modify result
            }    
            power *= 2; /* id like to use 2 ^ i instead of power but it
                         DOESNT give 1, 2, 4, 8, 16 ect... */
        }
        
        /* output */
        cout << "\nThe binary number " << str << " = " << result << "\n";
    } while (str[0] != 'q');   // do - while until "q" is typed 
    return 0;
}
1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

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 and #include.
well plz if you can help me in this case i will be very thankful of you...

Fakhre!

fakhre
Newbie Poster
3 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

First, when you post some code, use code tags:
http://www.daniweb.com/techtalkforums/announcement.php?f=8&announcementid=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:

#include<stdio.h>

char binary[8]; // unused, but if you need to store it, it will be in here
char str[8];    // input string
int result;     // integer result
int power = 1;  // power of 2. This is needed because of (2 ^ i) problem (see below)

int main(void)
{
    do  
    {
        printf ("Please input an 8 bit binary number or type q to quit:");
        scanf ("%s", str);
    
        result = 0; // reset to 0
        power = 1;  // first power = 2 ^ 0 = 1
        
        /* convert it */
        for(int i = 0; i < 8; i++)
        {
            if(str[i] == '1')
            {
                binary[i] = 1;// make binary number
                result += power; // modify result
            }    
            power *= 2; /* id like to use 2 ^ i instead of power but it
                         DOESNT give 1, 2, 4, 8, 16 ect... */
        }
        
        /* output */
        printf ("\nThe binary number %s = %d\n\n", str, result);
    } while (str[0] != 'q');   // do - while until "q" is typed 
    
    return 0;
}
frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

well my effort is just like this

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.

#include <stdio.h>

int main(void)
{
   int binary   = 0;    /* initialize the result to zero */
   int bitvalue = 1;    /* starting place value for bits */
   int decimal  = 1011; /* "decimal" value to "convert"  */
   while ( decimal )
   {
      binary   += decimal % 10 * bitvalue; /* get new bit and add to result */
      bitvalue *= 2;                       /* adjust base-2 place value     */
      decimal  /= 10;                      /* get next "decimal"            */
   }
   printf("binary = %d\n", binary);
   return 0;
}

/* my output
binary = 11
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

dave havent you just done DECIMAL to BINARY?? i thought we were doing BINARY to DECIMAL ???

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 
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).

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

here`s the code:

int main()
{
clrscr();
int b,d=0,a[100];
for(int z = 0;z<100;z++)        //initialising
 {
  a[z] = 0;
 }

cout<<"enter the binary no."<<endl;
cin>>b;
int c = 0,p;
while(b>0)
{
 int r = b%10;
 b = b/10;
 p = pow(2,c);
 d = d + (r*p);
 a[c] = r;
 c++;
}
cout<<"the decimal equivalent is "<<d<<endl;
return 0;
}
varunrathi
Light Poster
41 posts since Aug 2004
Reputation Points: 10
Solved Threads: 1
 

here`s the code:

cout<<"enter the binary no."<>b; cout<<"the decimal equivalent is "<My, what strange C you write. And use code tags : [code][/code]

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

There is an existing code snippet on this ...

http://www.daniweb.com/code/snippet109.html

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

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.

void main()
{
int a,b,c;
printf("enter intger number");
scanf("%d",&a);
while(a>0)
{ b=a%2;

a=a/10;
printf("%d",a);
}
getch();
}

dkb
Newbie Poster
1 post since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

differnce in stdio.h and conio.h in c language

prashantshekher
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

seems like a lot of solutions here. hope that they are useful fakhre, i tested mine a lot and still no errors but i didnt know about the pow function, it could simplify mine a bit (remove power, change 2^i with pow(2, i) i think) :)

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

Argh! Why must you people fill my brain with hurting!? And people ask me why I'm angry all of the time. I direct rage at anyone who insults the effort I put into learning how to use C properly.

Too much bad code, going somewhere else to cool off now...

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>Argh! Why must you people fill my brain with hurting!?
>Too much bad code

:sad: :cry:

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You