Home » Core Java » util » zip » Calculate CRC32 checksum for byte array

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Calculate CRC32 checksum for byte array

In this example we shall show you how to calculate the CRC32 Checksum of a byte array. To calculate the CRC32 checksum of a byte array one should perform the following steps:

  • Get the byte array of a String, using getBytes() API method of String.
  • Create a new Checksum object, that represents a data checksum.
  • Update the current checksum with the specified array of bytes, using update(byte[] b, int off, int len) API method of Checksum.
  • Get the current checksum long value, using getValue() API method of Checksum,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.zip.CRC32;
import java.util.zip.Checksum;

public class CalculateCRC32ChecksumForByteArray {
	
	public static void main(String[] args) {
		
		String input = "Java Code Geeks - Java Examples";
		 
		// get bytes from string
		byte bytes[] = input.getBytes();
		 
		Checksum checksum = new CRC32();
		
		// update the current checksum with the specified array of bytes
		checksum.update(bytes, 0, bytes.length);
		 
		// get the current checksum value
		long checksumValue = checksum.getValue();
		 
		System.out.println("CRC32 checksum for input string is: " + checksumValue);
		
	}

}

Output:

CRC32 checksum for input string is: 3564377865

 
This was an example of how to calculate the CRC32 checksum of a byte array in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Haris Jasarevic
Haris Jasarevic
2 years ago

how to Calculate CRC8 checksum for byte array! Thank you in advance.