C 中的 CRC 实现

问题描述 投票:0回答:1

我发现了用 C 语言实现的 CRC https://www.scaler.com/topics/crc-program-in-c/

#include<stdio.h>
#include<math.h>
#include<string.h>
#define ull unsigned lli
#define lli long long int
int Decimal_to_Binary(int decimal)
{
// binary value
    ull binary = 0;
// counter
    int count = 0;
// remainder variable
    int rem=0; 
    while (decimal != 0) {
// perform modulo operation
        rem = decimal % 2;
// compute power of 10
        ull tmp = pow(10, count);
// add result to compute the binary number
        binary += rem * tmp;
// divide the decimal by 2
        decimal /= 2;
// increment count
        count++;
    }
    return binary;
}
 
lli Binary_to_Decimal(char binary[]){
    lli decimal = 0;
// Use bit shifting with 1 to compute the number
// sum the numbers to get the decimal equivalent
    for (int i=0; i<strlen(binary); i++){
        if (binary[i]=='1')
            decimal += 1 << (strlen(binary) - i - 1);
    }
    return decimal;
}
 
// function to compute CRC and codeword
void CRC(char data[], char gen_poly[]){
// length of the generator polynomial
    int length_gen = strlen(gen_poly);
    printf("%d \n", length_gen);
// convert generator polynomial from binary to decimal
    lli generator_dec = Binary_to_Decimal(gen_poly);
    printf("%lld \n", generator_dec);
// Convert data from binary to decimal
    lli data_dec = Binary_to_Decimal(data);
    printf("%lld\n", data_dec);
// Shift n-1 bits to the left in data to append zeros
    lli dividend = data_dec << (length_gen-1); 
    printf("%lld\n", dividend);
// find the number of bits to shift for further computation.
    int shift_bits = (int) ceill(log2l(dividend+1)) - length_gen; 
    printf("%d \n", shift_bits);
// initialize variable for CRC or check value
    lli check_value;
// loop to find the check_value or CRC
    while ((dividend >= generator_dec) || (shift_bits >= 0)){
 // take the first four bits of the data 
 // perform XOR with the generator polynomial
        check_value = (dividend >> shift_bits) ^ generator_dec;               
// find the remainder of the operation
        dividend = (dividend & ((1 << shift_bits) - 1)) | (check_value << shift_bits);
// compute the number of bits to shift again
        shift_bits = (int) ceill(log2l(dividend + 1)) - length_gen;
    }
 // append the check value with the data
    lli final_data = (data_dec << (length_gen - 1)) | dividend;
// convert the decimal value to binary 
    printf("Check value or CRC: %d\n",Decimal_to_Binary(dividend));
// print the data to be transmitted
    printf("Data to be sent:  %d",Decimal_to_Binary(final_data));
}
 
int main(){
// Get the data
    char dataword[64];
    printf("Enter the data to be transmitted: ");
    scanf("%s", dataword);
// Get the generator polynomial
    char generator[64];
    printf("\nEnter the generator polynomial: ");    
    scanf("%s",generator);
// Calling the CRC function
    CRC(dataword, generator);
    return 0;
}

当我测试它时,编码和解码适用于例如数据= 0b1001101和生成器= 0b1011,但是当我在更大的数据上尝试它时,例如数据= 0b011010010111101011101010和生成器= 0b10110101它在解码后不会返回零。为什么会出现这种情况?

c crc
1个回答
0
投票

基本上,这是糟糕的代码。主要问题是,它将一串位表示为十进制数,每个 ten's 位置都有 0 或 1。所以你的位串

0b011010010111101011101010
需要76位来表示,整数或双精度类型都无法足够准确地表示。

你应该编写自己的代码。

© www.soinside.com 2019 - 2024. All rights reserved.