I have a 16 byte byte array with various values populated inside of it. Those values can and do cross the byte boundary.
Here is the binary representation of my data
0011 | 0011 | 1110 | 1000 | 1100 | 1000 | 1100 | 1000 | 0100 | 1101 | 0010 | 1111 | 1010 | 0001 | 1111 | 1111 [ 0 - 63]
0000 | 0101 | 0000 | 0011 | 0000 | 0011 | 1110 | 1000 | 1100 | 1000 | 1100 | 1000 | 0000 | 0001 | 0010 | 1100 [64 -127]
The various values I need to retrieve are stored in the following bit positions
0-3 | 4-5 | 6-15 | 16-23 | 24-31 | 32 - 63 | 64-71 | 72-79 | 80-83 | 84-85 | 86-94 | 96-103 | 104-111 | 112-127
CODE:
Here's the code to populate the Byte Array:
protected byte[] createMessageHeader(byte[] content) {
int[] set = new int[128];
set = intToBinary(set, 3, 3);
set = intToBinary(set, 0, 5);
set = intToBinary(set, 1000, 15);
set = intToBinary(set, 200, 23);
set = intToBinary(set, 200, 31);
set = intToBinary(set, 1294967295, 63);
set = intToBinary(set, 5, 71);
set = intToBinary(set, 3, 79);
set = intToBinary(set, 0, 83);
set = intToBinary(set, 0, 85);
set = intToBinary(set, 1000, 95);
set = intToBinary(set, 200, 103);
set = intToBinary(set, 200, 111);
set = intToBinary(set, 300, 127);
BitSet bitSet = binArrayToBitset(set);
byte[] b1 = bitSet.toByteArray();
for(int i = 0; i < b1.length; i++) {
b1[i] = (byte) (Integer.reverse(b1[i]) >>> 24);
}
return b1;
}
protected int[] intToBinary(int[] binary, int val, int start) {
// Number should be positive
while (val > 0) {
binary[start--] = val % 2;
val = val / 2;
}
return binary;
}
protected BitSet binArrayToBitset(int[] binArray) {
BitSet set = new BitSet(128);
for(int i = 0; i < binArray.length; i++) {
if(binArray[i] != 0)
set.set(i);
}
return set;
}
//Convenience method to print binary representation of values
protected void toBinString(int[] set) {
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < set.length; i++) {
if(i % 4 == 0)
stringBuilder.append("|");
if(i % 64 == 0)
stringBuilder.append("\n");
stringBuilder.append(set[i]);
}
}
The above code should produce the Byte Array with the specified values at the specific bit index ranges.
I have tried numerous methods for retrieving these values, most recently:
private int extractBits2(byte[] header, int start, int end) {
BitSet bitSet = BitSet.valueOf(header);
return (int) bitSet.get(start, end + 1).toLongArray()[0];
}
Using the above method, if I call it with:
int extracted = extractBits2(header, 6, 15)
The value returned is '0b00000011_10100000' (IntelliJ Debugger window), but I thought it would return '0b0011 1110 1000' (int 1000).
What am I missing here? I need to be able to retrieve the range of bits so I can validate their values.
NOTE: all the values stored are integers except for one which is a timestamp value (long).
set = intToBinary(set, 0);
is missing the 'start' argument. The 'content' parameter is unused.set = intToBinary(set, 1000, 95);
does not coincide with 'start'ing at the end of one of your declared positions (should be 94?) Maybe tidy it up a little and if there is a mistake it may reveal itself while doing so? Given the rather complicated means of setting up this array in the first place, can that be simplified?