You could define a function like:
char bitsToChar(const char *bits)
{
char result = 0;
for (int i = 0; i < 8; i++)
{
result = (result << 1) | bits[i];
}
return result;
}
In each loop, the next bit is appended to the right of the stored result, "pushing" the previously added bits to the left.
So this code:
#include <iostream>
using namespace std;
char bits[5][8] = {
{0,1,1,0,1,0,0,0},
{0,1,1,0,0,1,0,1},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,1,1} };
char bitsToChar(const char *bits)
{
char result = 0;
for (int i = 0; i < 8; i++)
{
result = (result << 1) | bits[i];
}
return result;
}
int main(const char* argv[], const int argc)
{
for (int j = 0; j < 5; j++)
{
for (int i = 0; i < 8; i++)
{
cout << (int)bits[j][i];
}
cout << " -> " << bitsToChar(bits[j]) << endl;
}
}
Produces the following output:
01101000 -> h
01100101 -> e
01101100 -> l
01101100 -> l
01101111 -> o