I want to convert
char lineOneC[8] = {0,1,1,0,0,0,0,1};
into
byte lineOneB = B01100001;
How do I do this in C++ / Arduino?
I'm not sure about specific restrictions imposed by the Adruino platform, but this should work on any standard compiler.
char GetBitArrayAsByte(const char inputArray[8])
{
char result = 0;
for (int idx = 0; idx < 8; ++idx)
{
result |= (inputArray[7-idx] << idx);
}
return result;
}
A test of this code is now on Codepad, if that helps.
If the result is supposed to be B01100001, then byte 0 is the MSB (most significant bit), not byte 7 ...
char line[8] = { 0, 1, 1, 0, 0, 0, 0, 1 };
unsigned char b = 0;
for ( int ii = 0; ii < 8; ii++ )
{
b <<= 1;
b |= line[ii];
}
The other answers I've seen, if I read them correctly, put the MSB on byte 7.
EDIT: Fixed quotes; miscopied it before.