Another method could be this, which is more flexible:
#include <iostream>
#include <string>
#include <math.h>
std::string BinaryTextToASCIIText(const std::string& binaryText, const unsigned int blockSize = 8, const unsigned int separatorLength = 1)
{
std::string text(""), block;
unsigned int separatorBlockStartIndex = blockSize;
for (unsigned int i = 0; i < binaryText.length(); i++)
{
if (i == separatorBlockStartIndex)
{
i += separatorLength;
separatorBlockStartIndex += blockSize + separatorLength;
}
block += binaryText[i];
if (block.length() == 8)
{
char binaryTextToASCIIChar = 0;
for (unsigned int j = 0; j < block.length(); j++)
{
if (block[j] == '1')
{
binaryTextToASCIIChar += pow(2, block.length() - j - 1);
}
}
block.clear();
text += binaryTextToASCIIChar;
}
}
return text;
}
int main()
{
//if you have your binary text in blocks of 32 bits and the blocks have been separated with " " (the separator can be any kind of ascii character and any length) you can use the function like this:
std::cout << BinaryTextToASCIIText("01110100011001010111001101110100", 32, 1) << std::endl;
//here is an example of blocks of 32 bits and the "0101111011010" (13 characters long) separator
std::cout << BinaryTextToASCIIText("010101000110100001101001011100110101111011010001000000110100101110011001000000101111011010011000010010000001110100011001010101111011010011100110111010000100001", 32, 13);
return 0;
}
The method can be much more useful, of course.. can be improved for sure, but works pretty good.