How to get the Checksum of a Byte Array in Java?


Create a Byte Array for which you want the Checksum −

byte[] arr = "This is it!".getBytes();

Now, create a Checksum object −

Checksum checksum = new Adler32();
checksum.update(arr, 0, arr.length);

The update() above updates the current checksum with the specified array of bytes.

Now, get the checksum with getValue() method, which gives the current checksum value.

Example

 Live Demo

import java.util.zip.Adler32;
import java.util.zip.Checksum;
public class Demo {
   public static void main(String[] argv) throws Exception {
      byte[] arr = "This is it!".getBytes();
      Checksum checksum = new Adler32();
      checksum.update(arr, 0, arr.length);
      long res = checksum.getValue();
      System.out.println("Checksum of a Byte array = "+res);
   }
}

Output

Checksum of a Byte array = 391709619

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

884 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements