永发信息网

C的Crc32 代码转成java的 crc32 代码 不用java内置的,该怎么解决

答案:1  悬赏:30  手机版
解决时间 2021-11-08 21:25
  • 提问者网友:几叶到寒
  • 2021-11-08 05:33
C的Crc32 代码转成java的 crc32 代码 不用java内置的,该怎么解决
最佳答案
  • 五星知识达人网友:千杯敬自由
  • 2021-11-08 06:22
public class Crc32 {
private static long[] crc32Table = new long[256];

static {
long crcValue;
for (int i = 0; i < 256; i++) {
crcValue = i;
for (int j = 0; j < 8; j++) {
if ((crcValue & 1) == 1) {
crcValue = crcValue >> 1;
crcValue = 0x00000000edb88320L ^ crcValue;
} else {
crcValue = crcValue >> 1;

}
}
crc32Table[i] = crcValue;
}

}

public static long getCrc32(byte[] bytes) {
long resultCrcValue = 0x00000000ffffffffL;
for (int i = 0; i < bytes.length; i++) {
int index = (int) ((resultCrcValue ^ bytes[i]) & 0xff);
resultCrcValue = crc32Table[index] ^ (resultCrcValue >> 8);
}
resultCrcValue = resultCrcValue ^ 0x00000000ffffffffL;
return resultCrcValue;
}

public static void main(String[] args) {
String testStr = "asfkjfkasdjfkldjfhdjfhasdjkfj;asdkljfk;asdjfcnnd";
java.util.zip.CRC32 jdkCrc32 = new java.util.zip.CRC32();
jdkCrc32.update(testStr.getBytes());
System.out.println("jdk  crc32: " + jdkCrc32.getValue());
System.out.println("test crc32: " + getCrc32(testStr.getBytes()));
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯