Java CRC32:与 C# 中的 CRC 不同
我必须将文件与 java 和 C# 脚本提供的 CRC32 代码进行比较。当我使用 java.util.zip.CRC32 计算 CRC32 时,结果完全不同...
我的猜测是 C# 脚本的多项式 = 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;
}
}
}
多谢, 坦率
I have to compare files with java with a CRC32 code provided by a C# script. When I calculate the CRC32 with java.util.zip.CRC32 the result is completely different ...
My guess is that the polynom = 0x2033 of the C# script is not the same as used in zip.CRC32. Is it possible to set the polynom ? Or any ideas of a java-class for calculating a CRC32 where you can define your own polynom?
UPDATE: problem is not the polymnom. This is the same between C# and Java
This is my code, maybe something is wrong in the way I read the file?
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;
}
}
}
Thanks a lot,
Frank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
标准 (IEEE) CRC32 多项式是 0x04C11DB7,它对应于:
这就是 java.util.zip.CRC32 使用的内容。不知道您提到的 C# 脚本...
您会发现此代码片段很有用:
The standard (IEEE) CRC32 polynomial is 0x04C11DB7 which corresponds to:
This is what java.util.zip.CRC32 uses. Don't know about the C# script you mention...
You can find this code snippet useful:
CRC-32 是根据 IEEE 802.3 的特定 CRC 变体,并使用多项式 0x04C11DB7。如果您的 C# 库使用多项式 0x2033,那么它/不是/CRC-32 的实现。
如果您需要 Java 代码来计算任意 CRC 变体,谷歌搜索“java crc”将为您提供几个示例。
CRC-32 is a specific CRC variant according to IEEE 802.3 and is using the polynom 0x04C11DB7. If your C# library is using the polynom 0x2033, it is /not/ an implementation of CRC-32.
If you need Java code to calculate arbitrary CRC variants, googling "java crc" will give you several examples.
通过从 C# 复制代码并将其转换为 Java 类来解决...
所以现在两者都使用相同的代码,只需对 unsigned <> 进行一些小的更改即可有符号字节差异。
Solved by copying the code from C# and convert it to a Java-class...
So now both use the same code, only had to do some minor changes for unsigned <> signed bytes differences.
1 + x + x^2 + x^4 + x^5 + x^7 + x^8 + x^10 + x^11 + x^12 + x^16 + x^22 + x^23 + x^ 26
(0x04C11DB7)
Java 使用上述多项式进行 CRC 32 计算,与 IEEE 802.3 标准不同,IEEE 802.3 标准还具有 32 位 x 的幂。
1 + x + x^2 + x^4 + x^5 + x^7 + x^8 + x^10 + x^11 + x^12 + x^16 + x^22 + x^23 + x^26
(0x04C11DB7)
Java uses the above polynomial for CRC 32 calculation and is different from IEEE 802.3 standard which has a 32 bit power of x in addition.