1

I have following problem: I'm reading 8 bit signal from one Arduino pin and store all informatin in bool array. Now I want to convert this array to single byte in decimal. How to do this?

I've tried this:

bool ID[8] = {0, 0, 0, 0, 0, 0, 1, 1};
int recivedID = int(ID);

and

bool ID[8] = {0, 0, 0, 0, 0, 0, 1, 1};
int recivedID = ID.toInt();

non works.

asked Aug 16, 2016 at 8:41
1
  • 1
    Why save it in a bool array in the first place? You could make a byte and set each bit individually. That will be more efficient. Commented Aug 16, 2016 at 9:16

2 Answers 2

0

try with this:

int recivedID = ID[0] | (ID[1] << 1) | (ID[2] << 2) | (ID[3] << 3) | (ID[4] << 4) | (ID[5] << 5) | (ID[6] << 6) | (ID[7] << 7);

which is the same as

int recivedID = 0;
for(int i=0; i<8; i++){
 recivedID |= ID[i] << i;
}

I haven't tested it though.

answered Aug 16, 2016 at 8:51
0
1
byte BoolArrayToByte(bool boolArray[8])
{
 byte result = 0; 
 for(int i = 0; i < 8; i++)
 {
 if(boolArray[i])
 {
 result = result | (1 << i);
 }
 }
 return result;
}
answered Aug 16, 2016 at 8:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.