-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc.h
60 lines (43 loc) · 1 KB
/
crc.h
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
57
58
59
60
#pragma once
#include "const.h"
#include "crc.h"
#include "windows.h"
#include <iostream>
#include <fstream>
using namespace std;
int toDec(int base, int val)
{
if (val == 0) return 1;
if (val == 1) return base;
int result = base;
for (int i = 2; i <= val; i++)
result = result * base;
return result;
}
char signCrc(int tab[], int half) {
int x = 0;
for (int i = 0; i < 8; i++)
x = x + toDec(2, i) * tab[(half * 8) - 1 - i];
return (char)x;
}
void calcCrc(char* tab) {
int sum = 0;
int count = 128;
while (--count >= 0)
{
sum = sum ^ (int)*tab++ << 8;
for (int i = 0; i < 8; ++i)
if (sum & 0x8000) sum = sum << 1 ^ 0x1021;
else sum = sum << 1;
}
sum = sum & 0xFFFF;
int x;
for (int z = 0; z < 16; z++) binary[z] = 0;
for (int i = 0; i < 16; i++)
{
x = sum % 2;
if (x == 1) sum = (sum - 1) / 2;
if (x == 0) sum = sum / 2;
binary[15 - i] = x;
}
}