HotEndUltiDecoder/HotEndUltiDecoder/Crc8.cs
2026-08-03 13:07:34 +02:00

36 lines
838 B
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder {
public static class Crc8 {
static readonly Byte[] table = new Byte[256];
// x8 + x7 + x6 + x4 + x2 + 1
const Byte poly = 0x07;
public static Byte ComputeChecksum(Byte[] bytes) {
Byte crc = 0;
if(bytes != null && bytes.Length > 0) {
foreach(Byte b in bytes) {
crc = table[crc ^ b];
}
}
return crc;
}
static Crc8() {
for(Int32 i = 0; i < 256; ++i) {
Int32 temp = i;
for(Int32 j = 0; j < 8; ++j) {
if((temp & 0x80) != 0) {
temp = (temp << 1) ^ poly;
} else {
temp <<= 1;
}
}
table[i] = (Byte)temp;
}
}
}
}