如何在 Java 中获取字节数组的校验和?
创建要获取校验和的字节数组 -
byte[] arr = "This is it!".getBytes();
现在,创建一个校验和对象 -
Checksum checksum = new Adler32(); checksum.update(arr, 0, arr.length);
上面的 update() 使用指定的字节数组更新当前校验和。
现在,使用 getValue() 方法获取校验和,它会提供当前校验和值。
示例
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);
}
}输出
Checksum of a Byte array = 391709619
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP