Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.0 kB
1
Indexable
Never
import java.util.*;

public class CFC {
    public static String message, generator;
    public static int[] data, divisor;
    static Scanner sc = new Scanner(System.in);

    public static int[] divide(int[] data, int[] divisor) {
        for (int i = 0; i < message.length(); i++) {
            if (data[i] == 1) {
                for (int j = 0; j < divisor.length; j++) {
                    data[i + j] ^= divisor[j];
                }
            }
        }
        return data;
    }

    public static void getInput() {
        System.out.println("Enter the Message: ");
        message = sc.nextLine();
        System.out.println("Enter Generator : ");
        generator = sc.nextLine();
        data = new int[message.length() + generator.length() - 1];
        divisor = new int[generator.length()];
        for (int i = 0; i < message.length(); i++) {
            data[i] = Integer.parseInt(message.charAt(i) + "");
        }
        for (int i = 0; i < generator.length(); i++) {
            divisor[i] = Integer.parseInt(generator.charAt(i) + "");
        }
    }

    public static void main(String[] args) {
        // Received code
        getInput();
        data = divide(data, divisor);

        // Display CRC
        System.out.println("The checksum code is: ");
        for (int i = 0; i < message.length(); i++) {
            data[i] = Integer.parseInt(message.charAt(i) + "");
        }
        for (int dt : data) {
            System.out.print(dt + " ");
        }
        System.out.println();

        // ------Transmitted code------
        System.out.println("Enter the received code: ");
        getInput();
        data = divide(data, divisor);

        // Error detection
        boolean valid = true;
        for (int x : data) {
            if (x == 1) {
                valid = false;
                break;
            }
        }
        if (valid == true) {
            System.out.println("No Error :) ");
        } else {
            System.out.println("Error Detected :( ");
        }
    }
}