mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 07:51:46 +02:00
Add help function for converting uint16 from bytes to int
This commit is contained in:
@@ -123,4 +123,10 @@ public class Converters {
|
||||
}
|
||||
return kg;
|
||||
}
|
||||
|
||||
public static int parseUnsignedInt16Be(byte[] data, int offset) {
|
||||
int value = (data[offset] & 0xFF) << 8;
|
||||
value += data[offset + 1] & 0xFF;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
@@ -79,4 +79,13 @@ public class ConvertersTest {
|
||||
Converters.toKilogram(Converters.fromKilogram(10.0f, unit), unit));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsignedIntConverters() throws Exception {
|
||||
byte[] data = new byte[]{(byte) 0xfd, (byte) 0xfe, (byte) 0xfc, (byte) 0x10, (byte) 0x7f};
|
||||
assertEquals(0xfdfe, Converters.parseUnsignedInt16Be(data, 0));
|
||||
assertEquals(0xfefc, Converters.parseUnsignedInt16Be(data, 1));
|
||||
assertEquals(0xfc10, Converters.parseUnsignedInt16Be(data, 2));
|
||||
assertEquals(0x107f, Converters.parseUnsignedInt16Be(data, 3));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user