Getting the CRC checksum of a byte array and adding it to that byte array

后端 未结 3 1882
庸人自扰
庸人自扰 2020-12-31 11:50

I have this byte array:

static byte[] buf = new byte[] { (byte) 0x01, (byte) 0x04, (byte)0x00, (byte)0x01,(byte)0x00, (byte) 0x01};

Now, th

3条回答
  •  既然无缘
    2020-12-31 12:32

    Would CRC32 do, or does it have to be CRC16? If 32 is okay, have you tried using the CRC32 in java.util.zip?

    import java.util.zip.CRC32;
    
    byte[] buf = new byte[] { (byte) 0x01, (byte) 0x04, (byte)0x00, (byte)0x01,(byte)0x00, (byte) 0x01};
    CRC32 crc32 = new CRC32();
    crc32.update(buf);
    System.out.printf("%X\n", crc32.getValue());
    

    The output is:

    F9DB8E67
    

    Then you can do whatever additional calculation you want on top of that.

提交回复
热议问题