Convert binary string to decimal number

Write a program to convert binary string to decimal number

Parse the input string till end. On encountering a “1” compute power of 2 for the current position and add to a variable holding the result.

C++ program to convert binary string to decimal number

#include <iostream>#include <cstring>#include <cstdlib>#include <cmath>using namespace std;int bin2dec(char* str) {int n = 0;int size = strlen(str) - 1;int count = 0;while ( *str != '' ) {if ( *str == '1' )n = n + pow(2, size - count );count++;str++;}return n;}int main() {char* bin_str = "1100100";cout << bin2dec(bin_str) << endl;}

Output:-

100