mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-13 18:41:14 +01:00
39 lines
1.2 KiB
Java
39 lines
1.2 KiB
Java
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
||
|
|
||
|
public class CheckSums {
|
||
|
public static int getCRC8(byte[] seq) {
|
||
|
int len = seq.length;
|
||
|
int i = 0;
|
||
|
byte crc = 0x00;
|
||
|
|
||
|
while (len-- > 0) {
|
||
|
byte extract = seq[i++];
|
||
|
for (byte tempI = 8; tempI != 0; tempI--) {
|
||
|
byte sum = (byte) ((crc & 0xff) ^ (extract & 0xff));
|
||
|
sum = (byte) ((sum & 0xff) & 0x01);
|
||
|
crc = (byte) ((crc & 0xff) >>> 1);
|
||
|
if (sum != 0) {
|
||
|
crc = (byte) ((crc & 0xff) ^ 0x8c);
|
||
|
}
|
||
|
extract = (byte) ((extract & 0xff) >>> 1);
|
||
|
}
|
||
|
}
|
||
|
return (crc & 0xff);
|
||
|
}
|
||
|
|
||
|
//thanks http://stackoverflow.com/questions/13209364/convert-c-crc16-to-java-crc16
|
||
|
public static int getCRC16(byte[] seq) {
|
||
|
int crc = 0xFFFF;
|
||
|
|
||
|
for (int j = 0; j < seq.length; j++) {
|
||
|
crc = ((crc >>> 8) | (crc << 8)) & 0xffff;
|
||
|
crc ^= (seq[j] & 0xff);//byte to int, trunc sign
|
||
|
crc ^= ((crc & 0xff) >> 4);
|
||
|
crc ^= (crc << 12) & 0xffff;
|
||
|
crc ^= ((crc & 0xFF) << 5) & 0xffff;
|
||
|
}
|
||
|
crc &= 0xffff;
|
||
|
return crc;
|
||
|
}
|
||
|
}
|