I want to analyze temperature from bytes.
The temperature consists of 2 bytes. The most significant bit of the first byte indicates whether the temperature is positive or negative.
This is what I have so far:
public double parseTemperatureBytes(byte[] temperatureBytes) {
byte firstByte = temperatureBytes[0];
int positiveOrNegative = ParseUtils.getMostSignificantBit(firstByte);
boolean isPositive = positiveOrNegative == 0;
String temperatureHex = ParseUtils.bytesToHex(temperatureBytes);
int temperatureHexToInteger = Integer.parseInt(temperatureHex, 16);
double temperature = temperatureHexToInteger / (double) 10;
if (!isPositive) {
temperature = -temperature;
}
return temperature;
}
public static int getMostSignificantBit(byte b) {
return (b & 0xff) >> 7;
}
This works, but I still need to make sure that I ignore the most significant bit of the first byte. This is just a flag, not part of the temperature.
Example: If I pass in 0xFFFF, it will return -6553.5, but it should be -3276.7
How can i achieve this?
source
share