Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.zip.CRC32; import java.util.zip.Checksum; public class JavaCRC32CheckSum { public static void main(String args[]) { String string = "CRC32 Checksum For Byte Array"; // Convert string to bytes byte bytes[] = string.getBytes(); Checksum checksum = new CRC32(); checksum.update(bytes, 0, bytes.length); long lngChecksum = checksum.getValue(); System.out.println("CRC32 checksum for byte array:" + lngChecksum); } } |
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.