From 90ea78f4898960fc26b02adb16b1b13e3a85d243 Mon Sep 17 00:00:00 2001 From: Maks Verver Date: Sat, 13 Oct 2018 14:45:08 +0200 Subject: [PATCH] Add Converters.toInt16Le() for symmetry with toInt16Be(). --- .../health/openscale/core/utils/Converters.java | 7 +++++++ .../java/com/health/openscale/ConvertersTest.java | 14 ++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java b/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java index fc3a5caf..01c2b31a 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java +++ b/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java @@ -247,6 +247,13 @@ public class Converters { return value; } + public static byte[] toInt16Le(int value) { + byte[] data = new byte[2]; + data[0] = (byte) (value & 0xFF); + data[1] = (byte) ((value >> 8) & 0xFF); + return data; + } + public static byte[] toInt16Be(int value) { byte[] data = new byte[2]; data[0] = (byte) ((value >> 8) & 0xFF); diff --git a/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java b/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java index 6deead16..f90ac557 100644 --- a/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java +++ b/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java @@ -82,7 +82,7 @@ public class ConvertersTest { } @Test - public void unsignedInt16Converters() throws Exception { + public void fromUnsignedInt16Converters() throws Exception { byte[] data = new byte[]{(byte) 0xfd, (byte) 0xfe, (byte) 0xfc, (byte) 0x10, (byte) 0x7f}; assertEquals(0xfefd, Converters.fromUnsignedInt16Le(data, 0)); @@ -94,12 +94,14 @@ public class ConvertersTest { assertEquals(0xfefc, Converters.fromUnsignedInt16Be(data, 1)); assertEquals(0xfc10, Converters.fromUnsignedInt16Be(data, 2)); assertEquals(0x107f, Converters.fromUnsignedInt16Be(data, 3)); + } - data = new byte[]{(byte) 0xff, (byte) 0xfe}; - assertArrayEquals(data, Converters.toInt16Be(0xfffe)); - assertEquals(0xffff, - Converters.fromUnsignedInt16Be( - Converters.toInt16Be(0xffff), 0)); + @Test + public void toInt16Converters() throws Exception { + assertArrayEquals(new byte[]{(byte) 0x12, (byte) 0x34}, Converters.toInt16Be(0x1234)); + assertArrayEquals(new byte[]{(byte) 0xff, (byte) 0xfe}, Converters.toInt16Be(0xfffe)); + assertArrayEquals(new byte[]{(byte) 0x34, (byte) 0x12}, Converters.toInt16Le(0x1234)); + assertArrayEquals(new byte[]{(byte) 0xfe, (byte) 0xff}, Converters.toInt16Le(0xfffe)); } @Test