- #include <stdio.h>
- #include <string.h>
- unsigned int crc_table[256];
- void generate_crc_table() {
- unsigned int crc, i, j;
- for (i = 0; i < 256; i++) {
- crc = i;
- for (j = 0; j < 8; j++) {
- if (crc & 0x01)
- crc = (crc >> 1) ^ 0xEDB88320;
- else
- crc >>= 1;
- }
- crc_table[i] = crc;
- }
- }
- unsigned int calculate_crc(const unsigned char *polynomial, const unsigned char *divisor) {
- unsigned int crc = 0xFFFFFFFF;
- unsigned int poly_length = strlen(polynomial);
- unsigned int divisor_length = strlen(divisor);
- for (unsigned int i = 0; i < poly_length; i++) {
- crc ^= polynomial[i];
- for (unsigned int j = 0; j < 8; j++) {
- if (crc & 0x01)
- crc = (crc >> 1) ^ crc_table[(crc ^ divisor[j]) & 0xFF];
- else
- crc >>= 1;
- }
- }
- return crc;
- }
- void print_binary(unsigned int value) {
- if (value > 1)
- print_binary(value >> 1);
- printf("%d", value & 1);
- }
- int main() {
- char polynomial[100];
- char divisor[100];
- printf("Enter the polynomial in binary: ");
- scanf("%s", polynomial);
- printf("Enter the divisor in binary: ");
- scanf("%s", divisor);
- generate_crc_table();
- unsigned int crc = calculate_crc((const unsigned char*)polynomial, (const unsigned char*)divisor);
- printf("CRC: ");
- print_binary(crc);
- printf("\n");
- return 0;
- }
[text] Cn2
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: