ByteAndBitConverter { public static Boolean[] toBoolean(String hex) throws IllegalArgumentException { if (hex.length() % 2 != 0) { throw new IllegalArgumentException("Illegal hex string"); } Boolean[] res = new Boolean[hex.length() * 4]; for (int i = 0; i < hex.length() / 2; i++) { String sub = hex.substring(2 * i, 2 * i + 2); int value = Integer.parseInt(sub, 16); int numOfBits = 8; for (int j = 0; j < numOfBits; j++) { boolean val = (value & 1 << j) != 0; res[8 * i + (numOfBits - j - 1)] = val; } } return res; } private  ByteAndBitConverter(); static byte[] toByteArray(long value); static byte[] toByteArray(int value); static BitSet intToBitSet(int i); static Boolean[] toBoolean(String hex); static String toHex(boolean[] bits); static String toHex(List<Boolean> bits);  }
@Test public void testToBoolean() { boolean exception = false; try { ByteAndBitConverter.toBoolean("Foo"); } catch (IllegalArgumentException e) { exception = true; } Assert.assertTrue(exception); Boolean[] x = ByteAndBitConverter.toBoolean("00"); Boolean[] expectedX = new Boolean[]{false, false, false, false, false, false, false, false}; Assert.assertEquals(expectedX.length, x.length); Assert.assertArrayEquals(expectedX, x); Boolean[] y = ByteAndBitConverter.toBoolean("42"); Boolean[] expectedY = new Boolean[]{false, true, false, false, false, false, true, false}; Assert.assertEquals(expectedY.length, y.length); Assert.assertArrayEquals(expectedY, y); Boolean[] z = ByteAndBitConverter.toBoolean("42FF"); Boolean[] expectedZ = new Boolean[]{false, true, false, false, false, false, true, false, true, true, true, true, true, true, true, true}; Assert.assertEquals(expectedZ.length, z.length); Assert.assertArrayEquals(expectedZ, z); }