我在下面函数中的意图是从文件的每个字节中提取最低有效位,并将其存储到字节数组中,但我一直在努力做到这一点。
在while循环中,我通过 &
然后我想把这个提取出来的位加到字节数组中。我不确定索引和将提取的位“附加”到数组中。
public byte[] extractLSB(File file, int size) {
FileInputStream fileInputStream = null;
byte[] lsbByteArray = new byte[size];
int arrayOffset = 0;
int dataByte, extractedLSB;
byte clearingByte = (byte) 0x01; // 0000 0001
try {
fileInputStream = new FileInputStream(file);
// Read byte by byte from the file input stream
while ((dataByte = fileInputStream.read()) != -1) {
// extract lsb and save it to the lsbByteArray
/*
//I've been trying something like this
extractedLSB = dataByte & clearingByte; // ? get lsb
lsbByteArray[arrayOffset] <<= 1; // make space for a new bit
lsbByteArray[arrayOffset] |= extractLSB; // "append" the lsb bit
arrayOffset++;
*/
}
fileInputStream.close();
} catch (Exception exception) {
exception.printStackTrace();
}
return lsbByteArray;
}
提前感谢您的帮助。
1条答案
按热度按时间mgdq6dx11#
您可以在while循环中使用字节计数器,除以8来查找字节索引,并使用模块8来查找位位置。
例如
这里我在数组中每8个源字节使用一个字节,因此counter/8来查找索引,字节中的位置是该除法的剩余部分,所以我执行shift extractedlsb<<(counter%8),最后还是用字节中已存储的其他位来计算结果。