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.
how to Calculate CRC8 checksum for byte array! Thank you in advance.