zip

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.

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.
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
5 years ago

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

Back to top button