- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
Advertisements