I have a string variable containing 32 bits of binary. What would be the best way to convert these 4 characters (8 bits is one character) represented by the binary back into the
An alternative if you're using C++11:
#include
#include
#include
#include
int main()
{
std::string data = "01110100011001010111001101110100";
std::stringstream sstream(data);
std::string output;
while(sstream.good())
{
std::bitset<8> bits;
sstream >> bits;
char c = char(bits.to_ulong());
output += c;
}
std::cout << output;
return 0;
}
Note that bitset is part of C++11.
Also note that if data is not well formed, the result will be silently truncated when sstream.good() returns false.