×

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also known as communication channels or digital data) and storage devices. It is used to track down unintentional modifications in digital data. This section will look at how to use a Java program to calculate and perform CRC. Let's take a closer look at CRC.

An error detection system assigns a unique number to each data block. The number is included primarily to identify changes that occur during transmission or storage. During data transmission, it is computed twice, once at the transmitter and again at the receiver. It compares data bit by bit with the original values delivered. If a data transmission error (corrupted bit) occurs, the CRC value does not match the value that was originally transferred. The graphic below helps us understand the CRC mechanism.

Fig:

CRC Program in Java

A single corrupted bit in the data, for example, causes a one-bit change in the calculated CRC, but many corrupted bits may cancel each other out. A burst error happens when a large number of bits are corrupted or changed at the same time.

Other error detection algorithms exist, such as Vertical Redundancy Check (VRC) and Longitudinal Redundancy Check (LRC). However, CRC is more powerful. Because CRC is calculated using binary division, the algorithm is more difficult. The divisor is calculated using polynomials. CRC is hence also known as a polynomial code checksum.

Steps for error detection in CRC:

  • In the first phase, N 0's are appended to the data unit. N is always less than the number of bits in the data unit (also known as division, which equals N+1).
  • The newly extended data is then split by a divisor using binary division, and the resultant reminder is known as the CRC remainder.
  • The remaining bits are then utilised to replace all of the 0's that had previously been added to the data unit. Following that, we handed the freshly created data unit to the receiver.
  • The receiver receives the data unit together with the CRC residual. The receiver then divides the data unit by the divisor.
  • If the leftover after dividing the data unit by the divisor is zero, the unit data is not corrupted and may be accepted.
  • If the leftover after dividing the data unit by the divisor is more than zero, the unit data is damaged and will be deleted.

Consider the following example with original data 11100 and divisor 1001.

  • First, we'll add the zeros in the data unit section. The length of the divisor is four, and we know that the length of the string 0s is always one less than the length of its divisor.
  •  Therefore, we increase the data unit by three zeros, yielding 11100. The result of adding the zeros is 11100000, which we divide by the divisor, which is 1001. We use the binary division approach to divide data by divisor.
  • The CRC remnant is the amount left behind after splitting the data unit by the divisor.
  • The CRC remainder substitutes the extra string of 0s at the end of the data unit, and the final string sent across the network is 11100111.

CrcDemo.java:

// import required classes and packages  
import java.io.*;
import java.util.*;  
// creating CrcDemo class to illustrate the working of CRC (*Cyclic Redundancy Check)  
class CrcDemo {  
   
    public static void main (String args []) {  
        // using Scanner Class object (* scan) to take user input  
        Scanner sc = new Scanner (System.in);  
        // declaring a size variable for data size.
        int size;  
        // user input for data size.
        System.out.println ("please enter a number for the size of the array: ");  
        size = sc.nextInt ();  
        // declaring the required data array  
        int dat [] = new int [size];  
        // take user input for data bits.  
        System.out.println ("please enter bits into the array : ");  
        for(int i = 0 ; i < size ; i++) {  
            System.out.println ("please enter bit " + (size-i) + ":");  
            dat [i] = sc.nextInt ();  
        }  
        // input the divisor size from the user  
        System.out.println ("please enter the divisor array size :");  
        size = sc.nextInt ();  
        // declaring  divisor array  
        int divArray [] = new int [size];  
        System.out.println ("please enter div bits into the array : ");  
        for(int i = 0 ; i < size ; i++) {  
            System.out.println ("please enter bit " + (size-i) + ":");  
            divArray [i] = sc.nextInt ();  
        }  
    // Dividing  input data with input divisor, store them  in  remdr array  
        int remdr [] = DataAndDivisorDivsion (dat, divArray);  
       
        for (int i = 0; i < remdr.length-1; i++) {  
            System.out.print (remdr [i]);  
        }  
        System.out.println ("\n CRC code that generated is: ");  
         
        for (int j = 0; j < dat.length; j++) {  
            System.out.print (dat [j]);  
        }  
        for (int k = 0; k < remdr.length-1; k++) {  
            System.out.print (remdr [k]);  
        }  
        System.out.println ();  
         
        // the size of the dataSent array with being equal to the sum of the data and the rem arrays length  
        int dataSent [] = new int[dat.length + remdr.length - 1];  
        System.out.println ("please enter bits needed to send into the array: ");  
        for (int i = 0; i < dataSent.length; i++) {  
            System.out.println ("please enter bit " + (dataSent.length - 1)+ ":");  
            dataSent [i] = sc.nextInt ();  
        }  
        recData (dataSent, divArray);  
    }  
    // creating DataAndDivisorDivsion () method to get Cyclic Redundancy Check  
    static int [] DataAndDivisorDivsion (int preDat [], int divArray []) {  
        // declaring remdr [] array  
        int remdr [] = new int [divArray.length];  
        int l;  
        int dat [] = new int [preDat.length + divArray.length];  
    // use the system's arraycopy () method for copying data into rem and data arrays  
        System.arraycopy (preDat, 0, dat, 0, preDat.length);  
        System.arraycopy (dat, 0, remdr, 0, divArray.length);  
        // iterate the preDat and exor the bits of the remainder and the divisor  
        for (l = 0; l < preDat.length; l++) {  
            System.out.println ((l+1) + ".) First data bit is : "+ remdr [0]);  
            System.out.print ("Remainder : ");  
            if (remdr [0] == 1) {  
                // exor the remainder bits with divisor bits  
                for (int j = 1; j < divArray.length; j++) {  
                    remdr [j-1] = exOrOpertn (remdr [j], divArray [j]);  
                    System.out.print (remdr [j-1]);  
                }  
            }  
            else {  
                // exor the remainder bits with 0  
                for (int j = 1; j < divArray.length; j++) {  
                    remdr [j-1] = exOrOpertn (remdr [j], 0);  
                    System.out.print (remdr [j-1]);  
                }  
            }  
            // The last bit of the remainder will be taken from the data  
            // This is the 'carry' taken from the dividend after every step  
            // of division  
            remdr [divArray.length-1] = dat [l + divArray.length];  
            System.out.println (remdr [divArray.length-1]);  
        }  
        return remdr;  
    }  
    // create exOrOpertn () method for exor operation on data  
    static int exOrOpertn (int x, int y) {  
        // returns exor of two bits  
        if (x == y) {  
            return 0;  
        }  
        return 1;  
    }  
    // method for received data  
    static void recData (int dat [], int divArray []) {  
       
        int remdr []  = DataAndDivisorDivsion (dat, divArray);  
        // Division complete
        for (int j = 0; j < remdr.length; j++) {  
            if (remdr [j] != 0) {  
                // if the remainder is not zero, then the data is corrupted  
                System.out.println (" received Currupted data...");  
                return;  
            }  
        }  
        System.out.println ("Data received without any error.");  
    }  
} 

Output:

CRC Program in Java CRC Program in Java

Related Topics

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.