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.
Just shift 0 or 1 to its position in binary format. Like this
char lineOneC[8] = {0,1,1,0,0,0,0,1};
char lineOneB = 0;
for(int i=0; i<8;i++)
{
lineOneB |= lineOneC[i] << (7-i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With