1
0
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:
Erik Johansson
2018-04-07 20:38:56 +02:00
parent 6899abd680
commit d132681b0d
2 changed files with 15 additions and 0 deletions

View File

@@ -123,4 +123,10 @@ public class Converters {
} }
return kg; return kg;
} }
public static int parseUnsignedInt16Be(byte[] data, int offset) {
int value = (data[offset] & 0xFF) << 8;
value += data[offset + 1] & 0xFF;
return value;
}
} }

View File

@@ -79,4 +79,13 @@ public class ConvertersTest {
Converters.toKilogram(Converters.fromKilogram(10.0f, unit), unit)); 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));
}
} }