mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-19 15:01:53 +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:
@@ -26,6 +26,7 @@ import com.health.openscale.R;
|
|||||||
import com.health.openscale.core.OpenScale;
|
import com.health.openscale.core.OpenScale;
|
||||||
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
||||||
import com.health.openscale.core.datatypes.ScaleUser;
|
import com.health.openscale.core.datatypes.ScaleUser;
|
||||||
|
import com.health.openscale.core.utils.Converters;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -275,13 +276,10 @@ public class BluetoothTrisaBodyAnalyze extends BluetoothCommunication {
|
|||||||
* encoded in little-endian byte order.</p>
|
* encoded in little-endian byte order.</p>
|
||||||
*/
|
*/
|
||||||
private void writeCommand(byte commandByte, int argument) {
|
private void writeCommand(byte commandByte, int argument) {
|
||||||
writeCommandBytes(new byte[]{
|
byte[] bytes = new byte[5];
|
||||||
commandByte,
|
bytes[0] = commandByte;
|
||||||
(byte) (argument >> 0),
|
Converters.toInt32Le(bytes, 1, argument);
|
||||||
(byte) (argument >> 8),
|
writeCommandBytes(bytes);
|
||||||
(byte) (argument >> 16),
|
|
||||||
(byte) (argument >> 24),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeCommandBytes(byte[] bytes) {
|
private void writeCommandBytes(byte[] bytes) {
|
||||||
|
@@ -247,17 +247,25 @@ public class Converters {
|
|||||||
return value;
|
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) {
|
public static byte[] toInt16Le(int value) {
|
||||||
byte[] data = new byte[2];
|
byte[] data = new byte[2];
|
||||||
data[0] = (byte) (value & 0xFF);
|
toInt16Le(data, 0, value);
|
||||||
data[1] = (byte) ((value >> 8) & 0xFF);
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] toInt16Be(int value) {
|
public static byte[] toInt16Be(int value) {
|
||||||
byte[] data = new byte[2];
|
byte[] data = new byte[2];
|
||||||
data[0] = (byte) ((value >> 8) & 0xFF);
|
toInt16Be(data, 0, value);
|
||||||
data[1] = (byte) (value & 0xFF);
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,21 +292,29 @@ public class Converters {
|
|||||||
return value;
|
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) {
|
public static byte[] toInt32Le(long value) {
|
||||||
byte[] data = new byte[4];
|
byte[] data = new byte[4];
|
||||||
data[3] = (byte) ((value >> 24) & 0xFF);
|
toInt32Le(data, 0, value);
|
||||||
data[2] = (byte) ((value >> 16) & 0xFF);
|
|
||||||
data[1] = (byte) ((value >> 8) & 0xFF);
|
|
||||||
data[0] = (byte) (value & 0xFF);
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] toInt32Be(long value) {
|
public static byte[] toInt32Be(long value) {
|
||||||
byte[] data = new byte[4];
|
byte[] data = new byte[4];
|
||||||
data[0] = (byte) ((value >> 24) & 0xFF);
|
toInt32Be(data, 0, value);
|
||||||
data[1] = (byte) ((value >> 16) & 0xFF);
|
|
||||||
data[2] = (byte) ((value >> 8) & 0xFF);
|
|
||||||
data[3] = (byte) (value & 0xFF);
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,8 @@ import com.health.openscale.core.utils.Converters;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.sql.ConnectionEvent;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
import static junit.framework.Assert.assertEquals;
|
||||||
import static junit.framework.Assert.assertFalse;
|
import static junit.framework.Assert.assertFalse;
|
||||||
import static junit.framework.Assert.assertTrue;
|
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) 0xff, (byte) 0xfe}, Converters.toInt16Be(0xfffe));
|
||||||
assertArrayEquals(new byte[]{(byte) 0x34, (byte) 0x12}, Converters.toInt16Le(0x1234));
|
assertArrayEquals(new byte[]{(byte) 0x34, (byte) 0x12}, Converters.toInt16Le(0x1234));
|
||||||
assertArrayEquals(new byte[]{(byte) 0xfe, (byte) 0xff}, Converters.toInt16Le(0xfffe));
|
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
|
@Test
|
||||||
@@ -114,7 +122,7 @@ public class ConvertersTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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};
|
byte[] data = new byte[]{(byte) 0xf1, (byte) 0xf2, (byte) 0xf3, (byte) 0x7f, (byte) 0x7e};
|
||||||
|
|
||||||
assertEquals(0x7ff3f2f1, Converters.fromUnsignedInt32Le(data, 0));
|
assertEquals(0x7ff3f2f1, Converters.fromUnsignedInt32Le(data, 0));
|
||||||
@@ -130,8 +138,11 @@ public class ConvertersTest {
|
|||||||
|
|
||||||
assertEquals(0x800001ffL, Converters.fromUnsignedInt32Be(data, 0));
|
assertEquals(0x800001ffL, Converters.fromUnsignedInt32Be(data, 0));
|
||||||
assertEquals(0x1ff00L, Converters.fromUnsignedInt32Be(data, 1));
|
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.toInt32Le(0xfcfdfeffL));
|
||||||
assertArrayEquals(data, Converters.toInt32Be(0xfffefdfcL));
|
assertArrayEquals(data, Converters.toInt32Be(0xfffefdfcL));
|
||||||
assertEquals(0xffffffffL,
|
assertEquals(0xffffffffL,
|
||||||
@@ -140,5 +151,11 @@ public class ConvertersTest {
|
|||||||
assertEquals(0xffffffffL,
|
assertEquals(0xffffffffL,
|
||||||
Converters.fromUnsignedInt32Be(
|
Converters.fromUnsignedInt32Be(
|
||||||
Converters.toInt32Be(0xffffffffL), 0));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user