Finding Decimal Equivalent of a Binary Number

Asked By 230 points N/A Posted on -
qa-featured

Hi there,

I am learning coding in C++. I would like to start, by making a full functional calculator.  All of them are pretty done but I want some more options which are present in a scientific calculator, like binary calculation.  I don't see any function for this. So I guess I have to make one.

I have been trying to add the binary to decimal conversion. I know that the basic process is:

But how do I take the digits 1 at a time.  Should I take the binary number as a string or by integer? Please help.

SHARE
Best Answer by AngelOfTech
Answered By 5 points N/A #95504

Finding Decimal Equivalent of a Binary Number

qa-featured

Sharon,

You don't need to use String as long as you are just considering binary and decimal ( the digits are within 0-9) . You can use  a integer.

To fetch the last digit. Use:

lastIDig=n%10;

and then when done calculation of the last digit. You can delete the last digit by simple integer division.

n/=10;

Hope this helps.

Answered By 230 points N/A #95506

Finding Decimal Equivalent of a Binary Number

qa-featured

Thank you very much.

But how do I know, how many digits are there in the number? Shall I fix it to be some where around 12 digit, by 0 padding to the left?

I am doing something like:

for(digit=1;digit<=numberofdigits;digit++)

    {
        result+=(n%10)*mult;
        mult*=2;
        n/=10;
    }

Best Answer
Best Answer
Answered By 5 points N/A #95508

Finding Decimal Equivalent of a Binary Number

qa-featured

You don't actually need to know the number of digits. Since you are dividing n by the base every time. It will eventually know when the conversion is done, by monitoring n( it would be 0 when done).

while(n>0)
    {
        result+=(n%10)*mult;
        mult*=2;
        n/=10;
    }

Answered By 230 points N/A #95510

Finding Decimal Equivalent of a Binary Number

qa-featured

Thanks a lot. That was  a pretty clean solution. So here are my 2 functions.

int binary_to_decimal(int n)
{
    int result=0,mult=1;
    while(n>0)
    {
        result+=(n%10)*mult;
        mult*=2;
        n/=10;
    }
    return result;
}

and

 int from_decimal_to_binary(int n)
    {
        int result=0,mult=1;
        while(n>0)
        {
            result+=(n%2)*mult;
            mult*=10;
            n/=2;
            }
            return result;
        }

Tell me if I can make any more upgrade.

Answered By 5 points N/A #95512

Finding Decimal Equivalent of a Binary Number

qa-featured

You are welcome.

Anyway you can extend this function to be much more generalized. For example this is the 2 generalized functions of yours:

int to_decimal(int n,int b)
{
    int result=0,mult=1;
    while(n>0)
    {
        result+=(n%10)*mult;
        mult*=b;
        n/=10;
    }
    return result;
}

 int from_decimal(int n,int b)
    {
        int result=0,mult=1;
        while(n>0)
        {
            result+=(n%b)*mult;
            mult*=10;
            n/=b;
            }
            return result;
        }

They act almost same as yours.  I am just passing the base here as a parameter. So it is capable of handling any base conversion ( base smaller than 10).  If you want to do a decimal to binary just issue:

from_decimal(n,2);     // 2 is the base for binary

for octal

from_decimal(n,8);     // same function with base 8.

Answered By 230 points N/A #95513

Finding Decimal Equivalent of a Binary Number

qa-featured

That was freaking awesome!!  Thank you very much.

Answered By 5 points N/A #95514

Finding Decimal Equivalent of a Binary Number

qa-featured

You are welcome.

Related Questions