-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
|
||
#define CRC8_POLYNOMIAL 0x07 | ||
|
||
// Function to generate CRC-8 lookup table | ||
void generate8(uint8_t table[256]) { | ||
for (uint16_t i = 0; i < 256; ++i) { | ||
uint8_t crc = i; | ||
for (size_t j = 1; j < 8; j++) { | ||
crc = (crc & 0x80) ? (crc << 1) ^ CRC8_POLYNOMIAL : crc << 1; | ||
} | ||
table[i] = crc; | ||
} | ||
} | ||
|
||
#define CRC16_POLYNOMIAL 0x8005 | ||
|
||
// Function to generate CRC-16 lookup table | ||
void generate16(uint16_t table[256]) { | ||
for (uint16_t i = 0; i < 256; ++i) { | ||
uint16_t crc = i; | ||
for (int j = 0; j < 8; ++j) { | ||
crc = (crc & 1) ? (crc >> 1) ^ CRC16_POLYNOMIAL : (crc >> 1); | ||
} | ||
table[i] = crc; | ||
} | ||
} | ||
|
||
int main() { | ||
// Example data | ||
// uint8_t data[] = "Hello, CRC!"; | ||
uint16_t table[256]; | ||
uint8_t table8[256]; | ||
|
||
generate8(table8); | ||
|
||
// Print the result | ||
for (size_t i=0; i<16; ++i) { | ||
for (size_t j=0; j<16; ++j) { | ||
// printf("0x%02X,", table8[i*16+j]); | ||
printf("%u,", table8[i*16+j]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
printf("--------------------------\n"); | ||
|
||
generate16(table); | ||
|
||
// Print the result | ||
for (size_t i=0; i<16; ++i) { | ||
for (size_t j=0; j<16; ++j) { | ||
printf("0x%04X,", table[i*16+j]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
/* | ||
crc = 0 | ||
for byte in message: | ||
crcByte = byte^crc | ||
crc = table[crcByte] | ||
*/ | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
// // CRC-8 polynomial (0x07) | ||
// #define CRC8_POLYNOMIAL 0x07 | ||
|
||
// // Function to calculate CRC-8 checksum | ||
// uint8_t crc8(const uint8_t *data, size_t length) { | ||
// uint8_t crc = 0; | ||
|
||
// for (size_t i = 0; i < length; ++i) { | ||
// crc ^= data[i]; | ||
|
||
// for (int j = 0; j < 8; ++j) { | ||
// crc = (crc & 0x80) ? (crc << 1) ^ CRC8_POLYNOMIAL : (crc << 1); | ||
// } | ||
// } | ||
|
||
// return crc; | ||
// } | ||
|
||
// // CRC-16 polynomial (0x8005) | ||
// #define CRC16_POLYNOMIAL 0x8005 | ||
|
||
// // Function to calculate CRC-16 checksum | ||
// uint16_t crc16(const uint8_t *data, size_t length) { | ||
// uint16_t crc = 0xFFFF; | ||
|
||
// for (size_t i = 0; i < length; ++i) { | ||
// crc ^= (uint16_t)data[i]; | ||
|
||
// for (int j = 0; j < 8; ++j) { | ||
// crc = (crc & 0x0001) ? (crc >> 1) ^ CRC16_POLYNOMIAL : (crc >> 1); | ||
// } | ||
// } | ||
|
||
// return crc; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters