[text] Cn1

Viewer

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. unsigned int crc_table[256];
  5.  
  6. void generate_crc_table() {
  7.     unsigned int crc, i, j;
  8.     
  9.     for (i = 0; i < 256; i++) {
  10.         crc = i;
  11.         
  12.         for (j = 0; j < 8; j++) {
  13.             if (crc & 0x01)
  14.                 crc = (crc >> 1) ^ 0xEDB88320;
  15.             else
  16.                 crc >>= 1;
  17.         }
  18.         
  19.         crc_table[i] = crc;
  20.     }
  21. }
  22.  
  23. unsigned int calculate_crc(const unsigned char *data, unsigned int length) {
  24.     unsigned int crc = 0xFFFFFFFF;
  25.     unsigned int i;
  26.     
  27.     for (i = 0; i < length; i++) {
  28.         crc = (crc >> 8) ^ crc_table[(crc ^ data[i]) & 0xFF];
  29.     }
  30.     
  31.     return ~crc;
  32. }
  33.  
  34. int main() {
  35.     char binaryString1[100];
  36.     char binaryString2[100];
  37.     
  38.     printf("Enter the first binary string: ");
  39.     scanf("%s", binaryString1);
  40.     
  41.     printf("Enter the second binary string: ");
  42.     scanf("%s", binaryString2);
  43.     
  44.     // Calculate CRC for binaryString1
  45.     generate_crc_table();
  46.     unsigned int crc1 = calculate_crc((const unsigned char*)binaryString1, strlen(binaryString1));
  47.     
  48.     // Calculate CRC for binaryString2
  49.     generate_crc_table();
  50.     unsigned int crc2 = calculate_crc((const unsigned char*)binaryString2, strlen(binaryString2));
  51.     
  52.     printf("CRC of binaryString1: %08X\n", crc1);
  53.     printf("CRC of binaryString2: %08X\n", crc2);
  54.     
  55.     return 0;
  56. }
  57.  

Editor

You can edit this paste and save as new:


File Description
  • Cn1
  • Paste Code
  • 02 Jun-2023
  • 1.39 Kb
You can Share it: