Convert Binary Stream/String to decimal number in C,C++
Again, there may be multiple way to doing it but I am listing one of the way which work for me.
Here is the code :
Here is the code :
#include <iostream>
#include <conio.h>
using namespace std;
int BinaryStream_To_Decimal(char* str)
{
int result = 0;
for (int len = strlen(str) - 1, count = 0; *str != '\0'; ++count, ++str)
{
if (*str == '1')
{
result += pow(2, len - count);
}
}
return result;
}
void main()
{
char* binaryString = "111";
cout << BinaryStream_To_Decimal(binaryString) << endl;
_getch();
}
Thanks for visiting.
Comments
Post a Comment