2

In Arduino IDE, I am placing all of input values to an array like so:

int eOb1 = digitalRead(PrOb1);
int eLoop = digitalRead(PrLoop);
int eOb2 = digitalRead(PrOb2);

InputValues[0] = eOb1;
InputValues[1] = eLoop;
InputValues[2] = eOb2;
InputValues[3] = 0;
InputValues[4] = 0;
InputValues[5] = 0;
InputValues[6] = 0;
InputValues[7] = 0;

I would like to convert it to a byte array like so: 00000111.
Can you show me please. I tried using a for Loop to iterate through the values but it doesn't work.

char bin[8];
for(int i = 0; i < 8; ++i) {
   bin &= InputValues[i];
}
8
  • What do you mean by byte array? I got confused when I started to answer. You want to get a byte having bits set as per InputValues? Commented Feb 28, 2018 at 6:31
  • I want to Serial.println the result as "0000111". The InputValues[0] to InputValues[2] can be 1 or 0 depending on the toggled switch. Commented Feb 28, 2018 at 6:32
  • Please see my updated code that incorporated your previous suggestion. Commented Feb 28, 2018 at 6:34
  • 1
    @stackquestions please do not change the question to incorporate a presented solution. It leaves the questions and answers in an inconsistent state. I am rolling back your edit for this one. Commented Feb 28, 2018 at 6:39
  • 2
    @stackquestions your last comment is very confusing. Do you want a string or a byte value. They are not like each other at all. Commented Feb 28, 2018 at 6:41

1 Answer 1

5

If I understand your requirement correctly, you have an array of individual bits and you need to convert it into a byte that has the corresponding bits.

So to start, you should declare bin to be of type unsigned char instead of char[8]. char[8] means an array of 8 bytes, whereas you only need a single byte.

Then you need to initialize it to 0. (This is important since |= needs the variable to have some defined value).

unsigned char bin;

Now, unsigned char is guaranteed to have 1 byte but not 8 bits. So you should use something like uint8_t IF it is available.

Finally you can set the appropriate bits in bin as -

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

There are two things I have changed.

  1. I used |= instead of &=. This is the bitwise OR operator. You need to use OR because it only sets the correct bits in the LHS and leaves other bits untouched. An AND won't necessarily set that bit and will also mask away (set to 0), the other bits.
  2. Shifted the bit in the array to the corresponding position using << i.
Sign up to request clarification or add additional context in comments.

4 Comments

It worked! But it is giving me 1 or 2 or 7 or 3. It's giving out hex. Can I have it as byte only like "0000101"?
uint8_t is always available in standard C, unless someone gets the not-too-brilliant idea to write code for various obsolete, oddball TI DSPs in C.
“have 1 byte but not 8 bits.” really?? Can you show me an example that unsigned char is 1 byte ,but not 8 bits?
@peter__barnes take a look at this question

Your Answer

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.