crc
This commit is contained in:
parent
2059134948
commit
38e229a644
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue