×

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

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

How to get Day Name from Date in Java

We'll write a Java application to extract the day's name from the Date in this section. When dealing with Date and time in Java, the following classes are used. Class for Calendars:...

6 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.