-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrc32_cal.c
More file actions
57 lines (44 loc) · 1.21 KB
/
crc32_cal.c
File metadata and controls
57 lines (44 loc) · 1.21 KB
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
#include <stdio.h>
#include <stdint.h>
// crc32 多项式: x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
#define CRC32_POLYNOMIAL 0xedb88320
// 生成crc32查表
void generate_crc32_table(uint32_t table[256]) {
uint32_t crc, i, j;
for (i = 0; i < 256; i++) {
crc = i;
// 对每一位执行模2除法
for (j = 0; j < 8; j++) {
// 检查最低位
if (crc & 1) {
crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
} else {
crc = crc >> 1;
}
}
// 存储结果
table[i] = crc;
}
}
int main() {
uint32_t crc_table[256];
int i, col = 0;
// 生成crc32表
generate_crc32_table(crc_table);
// 打印表格
printf("static const unsigned int crc_table[256] = {\n ");
// 按指定格式输出
for (i = 0; i < 256; i++) {
printf("0x%08xlL", crc_table[i]);
if (i < 255) {
printf(", ");
}
col++;
if (col == 5 && i < 255) {
printf("\n ");
col = 0;
}
}
printf("\n};\n");
return 0;
}