-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crc8.cs
52 lines (43 loc) · 1.47 KB
/
Crc8.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace OneWireAPI
{
internal class Crc8
{
private static byte[] _dataTable; // Lookup table of CRC8 values
static Crc8()
{
// Initialize the CRC lookup table
InitializeCrcTable();
}
private static void InitializeCrcTable()
{
// Initialize the size of the lookup table
_dataTable = new byte[256];
for (var outer = 0; outer < 256; outer++)
{
var accumulator = outer; // Accumulator value
var crc = 0; // CRC value
for (var inner = 0; inner < 8; inner++)
{
if (((accumulator ^ crc) & 0x01) == 0x01)
crc = ((crc ^ 0x18) >> 1) | 0x80;
else
crc = crc >> 1;
accumulator = accumulator >> 1;
}
_dataTable[outer] = (byte) crc;
}
}
public static short Calculate(byte[] data, int start, int end)
{
var currentCrc = (short) 0; // Current CRC accumulator
// Loop over all bytes in the input array
for (var index = start; index <= end; index++)
{
// Calculate the current CRC for this position
currentCrc = _dataTable[currentCrc ^ data[index]];
}
// Return the final CRC value
return currentCrc;
}
}
}