Binary To Decimal
This algorithm converts binary numbers to decimal numbers.
/*****Please include following header files*****/
// string
/***********************************************/
/*****Please use following namespaces*****/
// std
/*****************************************/
static int BinaryToDecimal(string bin) {
int binLength = bin.length();
double dec = 0;
for (int i = 0; i < binLength; ++i)
{
dec += (bin[i] - 48) * pow(2, ((binLength - i) - 1));
}
return (int)dec;
}
Example
string data = "10111101011011010111110";
int value = BinaryToDecimal(data);
Output
6207166