Binary To ASCII
This algorithm converts binary numbers to ASCII code.
/*****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;
}
static string BinaryToASCII(string bin) {
string ascii = "";
int binLen = bin.length();
for (int i = 0; i < binLen; i += 8)
{
ascii += (char)BinaryToDecimal(bin.substr(i, 8));
}
return ascii;
}
Example
string data = "01010000011100100110111101100111011100100110000101101101011011010110100101101110011001110010000001000001011011000110011101101111011100100110100101110100011010000110110101110011";
string value = BinaryToASCII(data);
Output
Programming Algorithms