Calculate CRC32b in Java

最后都变了- 提交于 2019-12-30 12:17:28

问题


I use java.util.zip.CRC32 which I understand implements CRC32 (and not CRC32b), but it seems like I need to use CRC32b instead of CRC32.

is there a Java open source code I can use for CRC32b calculation?


回答1:


CRC32b is a coined term if I remember correctly, equal to CRC32 with the 4 bytes reversed.

int crc32b(int crc) {
    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.putInt(crc); // BIG_ENDIAN by default.
    buf.order(ByteOrder.LITTLE_ENDIAN);
    return buf.getInt(0);
}

For instance for input '1':

byte b = (byte) '1';
CRC32 crc = new CRC32();
crc.update(b);

System.out.printf("%x%n", crc.getValue());
int finalCRC = crc32b((int)crc.getValue());
System.out.printf("%x%n", finalCRC);

Output:

83dcefb7
b7efdc83

Given your example the conclusion: java CRC32 (without reversal) is fine.



来源:https://stackoverflow.com/questions/25723365/calculate-crc32b-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!