Need a project done?

C++ Programming Developer

Search This Blog

Generic Binary to Decimal Converter in C++

#include <iostream>
#include <cmath> //For using pow operator which calculates x^y.
using namespace std;
int main()
{
int Binary;
cout<< "Binary: "; cin>>Binary;

//Seperating each digit of the Binary Number, since we have to work on each digit.
int Digit[11]; // An int cannot store more than 11 digits.
int k = 1, i = 0;
while (Binary/k != 0)
{
Digit[i] = (Binary/k) % 10;//Each digit of binary number is being stored in an array named Digit.
i++;
k *= 10; //k = k*10;
}

int Decimal = 0; i = 0;
while (Digit[i] != -858993460) //This is the garbage value which is produced Normally.
{
Decimal += Digit[i]*pow(2,i); //This is the formula which you and I use for the conversion from binary to decimal.
i++;
}
cout<< "Decimal: "<<Decimal<<endl;
return 0;
}

You might want to see: Generic Decimal to Binary Converter

Share knowledge: (Link to this post of decimal to binary program in C++ is: http://adf.ly/bjxQH)

1 comment:

"Don't let anyone ever make you feel like you don't deserve what you want."