0% found this document useful (0 votes)
39 views3 pages

CRC Method Using C

This C program implements the Cyclic Redundancy Check (CRC) method for error detection in data transmission. It calculates the CRC codeword by appending zeros to the data and performing XOR operations with a generator polynomial, and it also checks the validity of received data by verifying if the remainder is zero after performing the CRC check. The program prompts the user for input data, the generator polynomial, and received data to validate the integrity of the transmitted information.

Uploaded by

aditric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

CRC Method Using C

This C program implements the Cyclic Redundancy Check (CRC) method for error detection in data transmission. It calculates the CRC codeword by appending zeros to the data and performing XOR operations with a generator polynomial, and it also checks the validity of received data by verifying if the remainder is zero after performing the CRC check. The program prompts the user for input data, the generator polynomial, and received data to validate the integrity of the transmitted information.

Uploaded by

aditric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Implementing CRC method using C:

#include<stdio.h>
#include<string.h>
#define MAX_LEN 50 // Max length for data and polynomial
// Length of the generator polynomial
int N;
// Data to be transmitted and received
char data[MAX_LEN];
char check_value[MAX_LEN];
// Generator polynomial
char gen_poly[MAX_LEN];
// Function that performs XOR operation
void XOR() {
// If both bits are the same, the output is 0; if the bits are different, the output is 1
for (int j = 0; j < N; j++) {
check_value[j] = (check_value[j] == gen_poly[j]) ? '0' : '1';
}
}
// Function to perform CRC calculation (sender-side)
void crc() {
int data_len = strlen(data); // Length of the data
int i, j;
// Initializing check_value with data (append N-1 zeros to the end of the data)
for (i = 0; i < data_len; i++) {
check_value[i] = data[i];
}
for (i = data_len; i < data_len + N - 1; i++) {
check_value[i] = '0';
}
// Perform CRC calculation
for (i = 0; i < data_len; i++) {
if (check_value[i] == '1') { // If the current bit is 1, XOR with the generator polynomial
for (j = 0; j < N; j++) {
check_value[i + j] = (check_value[i + j] == gen_poly[j]) ? '0' : '1';
}
}
}
// Now, check_value contains the original data and CRC appended together
printf("CRC codeword: ");
for (i = 0; i < data_len; i++) {
printf("%c", data[i]);
}
for (i = data_len; i < data_len + N - 1; i++) {
printf("%c", check_value[i]);
}
printf("\n");
}
// Function to check if the CRC is valid at receiver side
int check_crc(char received_data[], char gen_poly[]) {
int data_len = strlen(received_data); // Length of the received data
int i, j;
// Initialize check_value with the received data
for (i = 0; i < data_len; i++) {
check_value[i] = received_data[i];
}
// Perform CRC check
for (i = 0; i < data_len - N + 1; i++) {
if (check_value[i] == '1') { // If the current bit is 1, XOR with the generator polynomial
for (j = 0; j < N; j++) {
check_value[i + j] = (check_value[i + j] == gen_poly[j]) ? '0' : '1';
}
}
}

// If the remainder is all 0's, the data is valid


for (i = data_len - N + 1; i < data_len; i++) {
if (check_value[i] != '0') {
return 0; // Data is invalid
}
}
return 1; // Data is valid
}
int main() {
printf("Enter the length of the generator polynomial: ");
scanf("%d", &N);
printf("Enter the data to be transmitted (in binary): ");
scanf("%s", data);
printf("Enter the generator polynomial (in binary): ");
scanf("%s", gen_poly);
// Perform CRC calculation and show the codeword
crc();
// Simulate the receiver-side validation of CRC
printf("\nReceiver-side validation...\n");
char received_data[MAX_LEN];
printf("Enter the received data (including CRC): ");
scanf("%s", received_data);
if (check_crc(received_data, gen_poly)) {
printf("CRC check passed! Data is valid.\n");
} else {
printf("CRC check failed! Data is corrupted.\n");
}
return 0;
}

You might also like