我必须将文件与 java 与 C# 脚本提供的 CRC32 代码进行比较。当我用 java.util.zip.CRC32 计算 CRC32 时,结果完全不同......
我的猜测是 C# 脚本的 polynom = 0x2033 与 zip.CRC32 中使用的不一样。是否可以设置多项式?或者任何关于计算 CRC32 的 java 类的想法,您可以在其中定义自己的多项式?
更新:问题不是多项式。这在 C# 和 Java 之间是一样的
这是我的代码,也许我读取文件的方式有问题?
package com.mine.digits.internal.contentupdater;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
public class CRC
{
public static String doConvert32(File file)
{
byte[] bytes = readBytesFromFile(file); // readFromFile(file).getBytes();
CRC32 x = new CRC32();
x.update(bytes);
return (Long.toHexString(x.getValue())).toUpperCase();
}
/** Read the contents of the given file. */
private static byte[] readBytesFromFile(File file)
{
try
{
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)
{
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
System.out.println("Could not completely read file " + file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
catch (IOException e)
{
System.out.println("IOException " + file.getName());
return null;
}
}
}
非常感谢,弗兰克