Am reading data out of a ByteBuffer
but the values I need are stored as 10 bits. ie:
1100101110 = 814
I've tried using BitSet
but this stores each byte in LSB first, which causes the final two bits to be incorrect the above example turns into:
1101001111 = 815
an example using a sequence of numbers:
8 bit 10 bit
01 02 03 04 05 06 07 08 | 09 10 | 11 12 13 14 15 16
would end up being:
8 bit 10 bit
08 07 06 05 04 03 02 01 | 16 15 | 14 13 12 11 10 09
So I can manage the first byte being in either order but the final two bits are taken from the wrong end of the next byte.
my current code is as follows:
// bb stands for the ByteBuffer in use.
//length stands for the number bytes covering the 10 bit numbers
BitSet data = getBitSet(bb, length);
int totalNumbers = data.length() / 10;
int[] numbers = new int[totalNumbers];
for (int i=0; i < totalNumbers; i++){
int start = i*10;
int end = (i+1)*10;
BitSet bs = data.get(start, end);
int tenBitNumber = 0;
for (int j = bs.nextSetBit(0); j >= 0; j = bs.nextSetBit(j+1)) {
double power = pow(2, 9-j);
tenBitNumber += power;
}
numbers[i] = tenBitNumber;
}
a worked example in Big Endian format: sequence of bytes:
11001011|10110111|00101100|11000111
which transforms when using BitSet
into:
11010011|11101101|00110100|11100011
What would be the best solution? I need to read multiple 10 bit length numbers from the ByteBuffer
.