This commit is contained in:
rusefillc 2023-10-21 23:46:20 -04:00
parent 2059134948
commit 38e229a644
2 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.rusefi.can.analysis;
public class J1850_SAE_crc8_Calculator {
byte crc8(byte[] data, int length) {
byte crc = 0;
if (data == null)
return 0;
crc ^= 0xff;
int ptr = 0;
while (length-- > 0) {
crc ^= data[ptr++];
for (int k = 0; k < 8; k++)
crc = (byte) (((crc & 0x80) != 0) ? (crc << 1) ^ 0x1d : crc << 1);
}
crc &= 0xff;
crc ^= 0xff;
return crc;
}
}

View File

@ -0,0 +1,14 @@
package com.rusefi.can.analysis;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class Crc8Test {
@Test
public void test() {
J1850_SAE_crc8_Calculator c = new J1850_SAE_crc8_Calculator();
assertEquals((byte) 0xfc, c.crc8(new byte[]{0x00, 0x00, 0x00, 0x00, (byte) 0xa0, 0x00, 0x2d}, 7));
}
}