2

I have a list of booleans which I want to convert Byte [] arrays.I try to convert List to byte[] arrays but I have a mistake.This is my code

 List<Boolean> list = model.getBooleanData();
               boolean[] inputSleep = new boolean[list.size()];

               byte[] toReturn = new byte[inputSleep.length / 8];
               for (int entry = 0; entry < toReturn.length; entry++) {
                   for (int bit = 0; bit < 8; bit++) {
                       if (inputSleep[entry * 8 + bit]) {
                           toReturn[entry] |= (128 >> bit);
                       }
                   }
               }

I get all Booleans and checked but I try to this all Booleans false.Thanks your helps.How can I correctly convert List to byte[] arrays?

7
  • 2
    boolean[] inputSleep - all values are initialised to false - you never fill the array with your boolean data from list. Have model.getBooleanData() return a boolean[] directly - the List is pointless. Commented Apr 14, 2018 at 9:35
  • But I fill List from model and checked the model is filled to booleans.And than I checked inputSleep all values False. Commented Apr 14, 2018 at 9:40
  • That's what I'm saying, and the reason why? Commented Apr 14, 2018 at 9:45
  • Can you be sure that 8 divides into the list length? Otherwise list elements will be lost. Commented Apr 14, 2018 at 9:52
  • Ooo sorry man,I understand now If I set the inputSleep from List Booleans all inputsleep values not to be false.I'll try ) Commented Apr 14, 2018 at 9:54

1 Answer 1

2

Yeap thank you guys I solved the problem. This is the right code. Maybe this solution needs someone)

 List<Boolean> list = model.getBooleanData();
               Boolean[] inputSleep = new Boolean[list.size()];
               inputSleep = list.toArray(inputSleep);


               byte[] toReturn = new byte[inputSleep.length / 8];
               for (int entry = 0; entry < toReturn.length; entry++) {
                   for (int bit = 0; bit < 8; bit++) {
                       if (inputSleep[entry * 8 + bit]) {
                           toReturn[entry] |= (128 >> bit);
                       }
                   }
               }
Sign up to request clarification or add additional context in comments.

Comments

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.