1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-18 14:31:23 +02:00

Create overloads of Converters.toInt* that write to an existing buffer.

This is symmetric with Converters.from*Int* which take a buffer and offset too.
This commit is contained in:
Maks Verver
2018-10-13 15:01:42 +02:00
parent 90ea78f489
commit d9885b967f
3 changed files with 52 additions and 21 deletions

View File

@@ -26,6 +26,7 @@ import com.health.openscale.R;
import com.health.openscale.core.OpenScale;
import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.core.utils.Converters;
import java.util.UUID;
@@ -275,13 +276,10 @@ public class BluetoothTrisaBodyAnalyze extends BluetoothCommunication {
* encoded in little-endian byte order.</p>
*/
private void writeCommand(byte commandByte, int argument) {
writeCommandBytes(new byte[]{
commandByte,
(byte) (argument >> 0),
(byte) (argument >> 8),
(byte) (argument >> 16),
(byte) (argument >> 24),
});
byte[] bytes = new byte[5];
bytes[0] = commandByte;
Converters.toInt32Le(bytes, 1, argument);
writeCommandBytes(bytes);
}
private void writeCommandBytes(byte[] bytes) {

View File

@@ -247,17 +247,25 @@ public class Converters {
return value;
}
public static void toInt16Le(byte[] data, int offset, int value) {
data[offset + 0] = (byte) (value & 0xFF);
data[offset + 1] = (byte) ((value >> 8) & 0xFF);
}
public static void toInt16Be(byte[] data, int offset, int value) {
data[offset + 0] = (byte) ((value >> 8) & 0xFF);
data[offset + 1] = (byte) (value & 0xFF);
}
public static byte[] toInt16Le(int value) {
byte[] data = new byte[2];
data[0] = (byte) (value & 0xFF);
data[1] = (byte) ((value >> 8) & 0xFF);
toInt16Le(data, 0, value);
return data;
}
public static byte[] toInt16Be(int value) {
byte[] data = new byte[2];
data[0] = (byte) ((value >> 8) & 0xFF);
data[1] = (byte) (value & 0xFF);
toInt16Be(data, 0, value);
return data;
}
@@ -284,21 +292,29 @@ public class Converters {
return value;
}
public static void toInt32Le(byte[] data, int offset, long value) {
data[offset + 3] = (byte) ((value >> 24) & 0xFF);
data[offset + 2] = (byte) ((value >> 16) & 0xFF);
data[offset + 1] = (byte) ((value >> 8) & 0xFF);
data[offset + 0] = (byte) (value & 0xFF);
}
public static void toInt32Be(byte[] data, int offset, long value) {
data[offset + 0] = (byte) ((value >> 24) & 0xFF);
data[offset + 1] = (byte) ((value >> 16) & 0xFF);
data[offset + 2] = (byte) ((value >> 8) & 0xFF);
data[offset + 3] = (byte) (value & 0xFF);
}
public static byte[] toInt32Le(long value) {
byte[] data = new byte[4];
data[3] = (byte) ((value >> 24) & 0xFF);
data[2] = (byte) ((value >> 16) & 0xFF);
data[1] = (byte) ((value >> 8) & 0xFF);
data[0] = (byte) (value & 0xFF);
toInt32Le(data, 0, value);
return data;
}
public static byte[] toInt32Be(long value) {
byte[] data = new byte[4];
data[0] = (byte) ((value >> 24) & 0xFF);
data[1] = (byte) ((value >> 16) & 0xFF);
data[2] = (byte) ((value >> 8) & 0xFF);
data[3] = (byte) (value & 0xFF);
toInt32Be(data, 0, value);
return data;
}
}

View File

@@ -20,6 +20,8 @@ import com.health.openscale.core.utils.Converters;
import org.junit.Test;
import javax.sql.ConnectionEvent;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
@@ -102,6 +104,12 @@ public class ConvertersTest {
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));
byte[] data = new byte[6];
Converters.toInt16Be(data, 1, 0x0102);
Converters.toInt16Be(data, 3, 0x0304);
Converters.toInt16Le(data, 2, 0x0506);
assertArrayEquals(new byte[]{ 0, 1, 6, 5, 4, 0}, data);
}
@Test
@@ -114,7 +122,7 @@ public class ConvertersTest {
}
@Test
public void unsignedInt32Converters() throws Exception {
public void fromUnsignedInt32Converters() throws Exception {
byte[] data = new byte[]{(byte) 0xf1, (byte) 0xf2, (byte) 0xf3, (byte) 0x7f, (byte) 0x7e};
assertEquals(0x7ff3f2f1, Converters.fromUnsignedInt32Le(data, 0));
@@ -130,8 +138,11 @@ public class ConvertersTest {
assertEquals(0x800001ffL, Converters.fromUnsignedInt32Be(data, 0));
assertEquals(0x1ff00L, Converters.fromUnsignedInt32Be(data, 1));
}
data = new byte[]{(byte) 0xff, (byte) 0xfe, (byte) 0xfd, (byte) 0xfc};
@Test
public void toInt32Converters() throws Exception {
byte[] data = new byte[]{(byte) 0xff, (byte) 0xfe, (byte) 0xfd, (byte) 0xfc};
assertArrayEquals(data, Converters.toInt32Le(0xfcfdfeffL));
assertArrayEquals(data, Converters.toInt32Be(0xfffefdfcL));
assertEquals(0xffffffffL,
@@ -140,5 +151,11 @@ public class ConvertersTest {
assertEquals(0xffffffffL,
Converters.fromUnsignedInt32Be(
Converters.toInt32Be(0xffffffffL), 0));
data = new byte[10];
Converters.toInt32Be(data, 1, 0x01020304);
Converters.toInt32Be(data, 6, 0x05060708);
Converters.toInt32Le(data, 3, 0x090a0b0c);
assertArrayEquals(new byte[]{ 0, 1, 2, 12, 11, 10, 9, 6, 7, 8}, data);
}
}