This commit is contained in:
rusefillc 2024-02-26 14:51:01 -05:00
parent 66eadaf43d
commit 8bd5e5c6f5
3 changed files with 38 additions and 6 deletions

View File

@ -113,6 +113,7 @@ void CanTxMessage::setBus(size_t bus) {
busIndex = bus;
}
// LSB Little-endian System, "Intel"
void CanTxMessage::setShortValue(uint16_t value, size_t offset) {
m_frame.data8[offset] = value & 0xFF;
m_frame.data8[offset + 1] = value >> 8;

View File

@ -53,7 +53,7 @@ public:
uint8_t& operator[](size_t);
/**
* @brief Write a 16-bit short value to the buffer. Note: this writes in big endian byte order.
* @brief Write a 16-bit short value to the buffer. Note: this writes in little endian byte order.
*/
void setShortValue(uint16_t value, size_t offset);

View File

@ -0,0 +1,31 @@
package com.rusefi.core;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class FileUtilTest {
static void setShortValue(byte[] data8, int value, int offset) {
data8[offset] = (byte) (value & 0xFF);
data8[offset + 1] = (byte) (value >> 8);
}
@Test
public void damnItWhichOneIsLittleEndian() {
byte[] testArrayForLittleEndianByteBuffer = new byte[2];
// LSB Little-endian System, "Intel"
ByteBuffer littleEndianBuffer = FileUtil.littleEndianWrap(testArrayForLittleEndianByteBuffer, 0, 2);
short testValue = 0x1122;
littleEndianBuffer.putShort(testValue);
assertEquals(testArrayForLittleEndianByteBuffer[0], 0x22);
byte[] testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue = new byte[2];
setShortValue(testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue, testValue, 0);
assertEquals(testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue[0], 0x22);
assertEquals(testArrayForJavaCodeWhichMimicsCanTxMessageSetShortValue[1], testArrayForLittleEndianByteBuffer[1]);
}
}