我有一个布尔列表,我想转换Byte []数组。我尝试将List转换为byte []数组,但我有一个错误。这是我的代码
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);
}
}
}
我得到所有布尔值并检查但我尝试将所有布尔值假。感谢你的帮助。如何才能正确地将List转换为byte []数组?
答案 0 :(得分:1)
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);
}
}
}