danieln
danieln

Reputation: 4973

Calculate CRC32b in Java

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?

Upvotes: 1

Views: 2064

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109547

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.

Upvotes: 2

Related Questions