cl1p.net - The internet clipboard
Login/Sign Up
cl1p.net/error
cl1p.net/error
Login/Sign Up
This cl1p will be deleted in in 19 days.
Copy
error detection using crc #include
#include
#define N 100 void xorOperation(char *dividend, const char *divisor) { for (int i = 0; i < strlen(divisor); i++) dividend[i] = (dividend[i] == divisor[i]) ? '0' : '1'; } void divide(char *data, const char *divisor, char *remainder) { int dataLen = strlen(data); int divisorLen = strlen(divisor); char temp[N]; strncpy(temp, data, divisorLen); temp[divisorLen] = '\0'; for (int i = 0; i <= dataLen - divisorLen; i++) { if (temp[0] == '1') xorOperation(temp, divisor); if (i + divisorLen < dataLen) memmove(temp, temp + 1, divisorLen - 1), temp[divisorLen - 1] = data[i + divisorLen]; } strcpy(remainder, temp); } void encode(char *data, const char *divisor) { int dataLen = strlen(data); int divisorLen = strlen(divisor); char temp[N]; strcpy(temp, data); strcat(temp, "000"); // Append zero bits char remainder[N]; divide(temp, divisor, remainder); for (int i = 0; i < divisorLen - 1; i++) data[dataLen + i] = remainder[i]; data[dataLen + divisorLen - 1] = '\0'; } int checkError(char *received, const char *divisor) { char remainder[N]; divide(received, divisor, remainder); return (strchr(remainder, '1') == NULL); } int main() { char data[N] = "1101"; char divisor[N] = "1011"; printf("Original Data: %s\n", data); encode(data, divisor); printf("Transmitted Codeword: %s\n", data); // Test without error printf("Received Codeword: %s\n", data); printf("Error Detection: %s\n", checkError(data, divisor) ? "No Error" : "Error Detected"); // Introduce error data[2] = (data[2] == '1') ? '0' : '1'; printf("Received Codeword (with error): %s\n", data); printf("Error Detection: %s\n", checkError(data, divisor) ? "No Error" : "Error Detected"); return 0; }