How to convert byte to boolean [8] and convert back?

7

I need to burn some booleans into a file, and then retrieve them. I can write and read one or more bytes with Classes java.io.FileInputStream and java.io.FileOutputStream , so I need to convert 1 byte to an array of 8 booleans, and convert one array of 8 booleans to 1 byte, like this:

public static boolean[] byteToBooleanArray(byte b) {
    boolean[] array = new boolean[8];
    //O que colocar aqui?
    return array;
}

public static byte booleanArrayToByte(boolean[] array) {
    if (array.length != 8) throw new IllegalArgumentException();
    byte b;
    //O que colocar aqui?
    return b;
}

Note: I wish I did not have to download a library.

    
asked by anonymous 01.07.2017 / 22:15

2 answers

6

I think there will be several ways to do this. The one that occurs to me now is:

  • Use the expression (b & (1 << i)) != 0 to check that the bit in the i position of the byte is "set" and save the result to its position in the array.

    public static boolean[] byteToBooleanArray(byte b) {
        boolean[] array = new boolean[8];
        for (int i=0; i<8 ;i++){
            array[i] = (b & (1 << i)) != 0;
        }
        return array;
    }
    
  • Use the expression b |= 1 << i to "set", in the byte, the bit in the position that corresponds to the position i of the array, if its value is true .

    public static byte booleanArrayToByte(boolean[] array) {
        if (array.length != 8) throw new IllegalArgumentException();
        byte b = 0;
        for (int i = 0; i < 8; i++) {
            if (array[i]) {
                b |= 1 << i;
            }
        }
        return b;
    }
    
01.07.2017 / 22:58
4

I believe converting the boolean array to a single byte involves many bite-to-bite (arithmetic shift) operations, which are often complex to understand and maintain, unless you have extensive experience with it. Probably the simplest result is an array of bytes, which you can use in the stream you want, as in the example below:

@Test
public void testConvert() throws Exception{
    boolean[] esperado = {true, false, false, true, true, false, false, true};
    byte[] result;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);

    oos.writeObject(esperado);

    oos.flush(); 
    result = bos.toByteArray();

    Assert.assertNotNull(result);
    System.out.println(result.length);

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(result));

    boolean[] obtido = (boolean[]) ois.readObject();

    for(int i = 0; i < esperado.length; i++){
        Assert.assertEquals(esperado[i], obtido[i]);
        System.out.println(esperado[i]);
    }
}

Remembering that instead of ByteArrayOutputStream you can use FileOutputStream directly or, as previously mentioned, any type of stream you want.

However, while I do not know exactly what you need this solution for, I think there might be more elegant ways to solve your problem.

I hope I have helped.

    
01.07.2017 / 23:01