I have a formula in a specification for a binary file. The spec gives details of the meaning of the various bytes in the heading.
In particular, one formula states this about 2 of the bytes:
Byte 1 --> 7 6 5 4 3 2 1 0
Byte 2 --> 7 6 5 4 3 2 1 0
R Roll
If 'R' = 0, Roll Angle not available
If 'R' = 1, Roll Angle = [((Byte 84 & 0x7F)<<8) | (Byte 85) – 900] / 10
I need to take a value such as 4.3 and convert it to two bytes such that it will be able to be converted back to 4.3 using the above formula. The part that puzzles me the most is subtracting the 900.
This is what I have so far:
private byte[] getRollBytes(BigDecimal[] positionData) {
BigDecimal roll = positionData[4];
roll = roll.multiply(BigDecimal.TEN);
roll = roll.add(new BigDecimal(900));
short shortval = roll.shortValue();
byte[] rollBytes = new byte[2];
ByteBuffer headingbuf = ByteBuffer.wrap(rollBytes);
headingbuf.order(ByteOrder.BIG_ENDIAN);
headingbuf.putShort(shortval);
//set the leftmost bit of the two bytes to 'on', meaning data is available
rollBytes[0] = (byte) (rollBytes[0] | (0x80));
//testing the result with my formula doesn't give me the right answer:
float testFloat = (float) (((((rollBytes[0] & 0x7F) <<8) | rollBytes[1]) - 900) /10);
return rollBytes;
}
I think something is getting lost in the conversion between short and byte...