0% found this document useful (0 votes)
5 views2 pages

CRC_cdaf607b5e47eb8cb040e23a6f7a9ac7

This C program implements Cyclic Redundancy Check (CRC) for error detection in binary messages. It encodes a message using a given divisor to generate a CRC code, appends it to the message, and checks for errors in the received message. The program outputs whether an error was detected or not after receiving the message.

Uploaded by

riteshstudy22
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)
5 views2 pages

CRC_cdaf607b5e47eb8cb040e23a6f7a9ac7

This C program implements Cyclic Redundancy Check (CRC) for error detection in binary messages. It encodes a message using a given divisor to generate a CRC code, appends it to the message, and checks for errors in the received message. The program outputs whether an error was detected or not after receiving the message.

Uploaded by

riteshstudy22
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/ 2

Write a C program to detect error using CRC.

Code:-
#include <stdio.h>
#include <string.h>

#define MAX 100

// Function to perform XOR operation


void xorOperation(char *dividend, char *divisor, int divisorLength) {
for (int i = 0; i < divisorLength; i++) {
dividend[i] = (dividend[i] == divisor[i]) ? '0' : '1';
}
}

// Function to perform CRC encoding


void crcEncode(char *message, char *divisor, char *crc) {
int messageLength = strlen(message);
int divisorLength = strlen(divisor);

// Append zeros to the message


char temp[MAX];
strcpy(temp, message);
for (int i = 0; i < divisorLength - 1; i++) {
temp[messageLength + i] = '0';
}
temp[messageLength + divisorLength - 1] = '\0';

// Perform the division


for (int i = 0; i < messageLength; i++) {
if (temp[i] == '1') {
xorOperation(temp + i, divisor, divisorLength);
}
}

// The CRC code is the last (divisorLength - 1) bits of the temp


strncpy(crc, temp + messageLength, divisorLength - 1);
crc[divisorLength - 1] = '\0'; // Null-terminate the CRC string
}

// Function to check for errors in the received message


int crcCheck(char *received, char *divisor) {
int receivedLength = strlen(received);
int divisorLength = strlen(divisor);

// Perform the division


for (int i = 0; i < receivedLength - divisorLength + 1; i++) {
if (received[i] == '1') {
xorOperation(received + i, divisor, divisorLength);
}
}

// If the remainder is all zeros, there is no error


for (int i = receivedLength - divisorLength + 1; i < receivedLength; i++) {
if (received[i] == '1') {
return 1; // Error detected
}
}
return 0; // No error
}

int main() {
char message[MAX], divisor[MAX], crc[MAX], received[MAX];

// Input the message and divisor


printf("Enter the binary message: ");
scanf("%s", message);
printf("Enter the divisor (polynomial): ");
scanf("%s", divisor);

// Encode the message


crcEncode(message, divisor, crc);
printf("CRC code: %s\n", crc);

// Create the transmitted message


strcat(message, crc);
printf("Transmitted message: %s\n", message);

// Simulate receiving the message


printf("Enter the received message: ");
scanf("%s", received);

// Check for errors


if (crcCheck(received, divisor)) {
printf("Error detected in the received message.\n");
} else {
printf("No error detected in the received message.\n");
}

return 0;
}
output:-

You might also like