-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecksum.cpp
56 lines (47 loc) · 966 Bytes
/
checksum.cpp
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
53
54
55
56
/* IMPLEMENTATIONS OF CHECKSUM FUNCTION USING CRC METHOD */
#include <iostream>
#include <stdint.h>
#include <cstdio>
#include "checksum.h"
using namespace std;
//get CRC value
char getCRC(char* message, int length)
{
int i, j;
char crc = 0;
for (i = 0; i < length; i++)
{
crc ^= message[i];
for (j = 0; j < 8; j++)
{
if (crc & 1)
crc ^= CRC8;
crc >>= 1;
}
}
return crc;
}
// check if a message contains CRC is valid
bool isValid(char* message, int size){
if (getCRC(message, size))
return false;
else
return true;
}
// void dec2bin(int c)
// {
// int i = 0;
// for(i = 31; i >= 0; i--){
// if((c & (1 << i)) != 0){
// printf("1");
// }else{
// printf("0");
// }
// }
// }
// int main(){
// char message[3] = {0x83, 0x01, 0x00};
// message[2] = 0;
// printf("%d\n",isValid(message, 3));
// return 0;
// }