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

心不动则不痛 提交于 2019-12-18 15:48:38

问题


I have this byte array:

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

Now, the CRC checksum of this byte array is supposed to be 0x60, 0x0A. I want the Java code to recreate this checksum, however I cant seem to recreate it. I have tried crc16:

static int crc16(final byte[] buffer) {
    int crc = 0xFFFF;

    for (int j = 0; j < buffer.length ; j++) {
        crc = ((crc  >>> 8) | (crc  << 8) )& 0xffff;
        crc ^= (buffer[j] & 0xff);//byte to int, trunc sign
        crc ^= ((crc & 0xff) >> 4);
        crc ^= (crc << 12) & 0xffff;
        crc ^= ((crc & 0xFF) << 5) & 0xffff;
    }
    crc &= 0xffff;
    return crc;

}

and convert them using Integer.toHexString(), but none of the results match the correct CRC. Could someone please point me in the right direction in terms of CRC formula.


回答1:


Use the following code instead:

// Compute the MODBUS RTU CRC
private static int ModRTU_CRC(byte[] buf, int len)
{
  int crc = 0xFFFF;

  for (int pos = 0; pos < len; pos++) {
    crc ^= (int)buf[pos] & 0xFF;   // XOR byte into least sig. byte of crc

    for (int i = 8; i != 0; i--) {    // Loop over each bit
      if ((crc & 0x0001) != 0) {      // If the LSB is set
        crc >>= 1;                    // Shift right and XOR 0xA001
        crc ^= 0xA001;
      }
      else                            // Else LSB is not set
        crc >>= 1;                    // Just shift right
    }
  }
// Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
return crc;  
}

You may have to reverse your return CRC to get the right endianness, though. I even tested it here:

http://ideone.com/PrBXVh

Using windows calculator or something you can see that the first result (from the above function call) gives the expected value (albeit reversed).




回答2:


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.




回答3:


I was working on modbus using Java 1.6, tried the above code and it only partially worked? Agreed on some CRCs, wrong on others. I researched it a bit more, and saw I had a problem with sign extension. I masked off the high bits (see FIX HERE below) and now it works great. NOTE: All CRC calcs are not the same, MODBUS is a bit different:

    public static int getCRC(byte[] buf, int len ) {
    int crc =  0xFFFF;
    int val = 0;

      for (int pos = 0; pos < len; pos++) {
        crc ^= (int)(0x00ff & buf[pos]);  // FIX HERE -- XOR byte into least sig. byte of crc

        for (int i = 8; i != 0; i--) {    // Loop over each bit
          if ((crc & 0x0001) != 0) {      // If the LSB is set
            crc >>= 1;                    // Shift right and XOR 0xA001
            crc ^= 0xA001;
          }
          else                            // Else LSB is not set
            crc >>= 1;                    // Just shift right
        }
      }
    // Note, crc has low and high bytes swapped, so use it accordingly (or swap bytes)
    val =  (crc & 0xff) << 8;
    val =  val + ((crc >> 8) & 0xff);
    System.out.printf("Calculated a CRC of 0x%x, swapped: 0x%x\n", crc, val);
    return val;  

}   // end GetCRC


来源:https://stackoverflow.com/questions/17474223/getting-the-crc-checksum-of-a-byte-array-and-adding-it-to-that-byte-array

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