mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-20 07:21:40 +02:00
Add method for converting from uint24 LE
This commit is contained in:
@@ -143,6 +143,13 @@ public class Converters {
|
||||
return data;
|
||||
}
|
||||
|
||||
public static int fromUnsignedInt24Le(byte[] data, int offset) {
|
||||
int value = (data[offset + 2] & 0xFF) << 16;
|
||||
value += (data[offset + 1] & 0xFF) << 8;
|
||||
value += data[offset] & 0xFF;
|
||||
return value;
|
||||
}
|
||||
|
||||
public static long fromUnsignedInt32Be(byte[] data, int offset) {
|
||||
long value = (long) (data[offset] & 0xFF) << 24;
|
||||
value += (data[offset + 1] & 0xFF) << 16;
|
||||
|
@@ -102,6 +102,15 @@ public class ConvertersTest {
|
||||
Converters.toUnsignedInt16Be(0xffff), 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsignedInt24Converters() throws Exception {
|
||||
byte[] data = new byte[]{(byte) 0xfd, (byte) 0xfe, (byte) 0xfc, (byte) 0x10, (byte) 0x7f};
|
||||
|
||||
assertEquals(0xfcfefd, Converters.fromUnsignedInt24Le(data, 0));
|
||||
assertEquals(0x10fcfe, Converters.fromUnsignedInt24Le(data, 1));
|
||||
assertEquals(0x7f10fc, Converters.fromUnsignedInt24Le(data, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsignedInt32Converters() throws Exception {
|
||||
byte[] data = new byte[]{(byte) 0xf1, (byte) 0xf2, (byte) 0xf3, (byte) 0x7f, (byte) 0x7e};
|
||||
|
Reference in New Issue
Block a user