Calculate CRC32b in Java

后端 未结 1 1143
無奈伤痛
無奈伤痛 2021-01-16 04:32

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

相关标签:
1条回答
  • 2021-01-16 04:50

    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.

    0 讨论(0)
提交回复
热议问题