Java crc16 implementation

I am having problems calculating the implementation of a CRC-16 byte array in java. Basically, I am trying to send bytes to an RFID that starts to write to a tag. I can see the array checksum value by looking at the tcpdump command on mac. But my goal is to create it yourself. Here is my byte array that should generate 0xbe, 0xd9:

byte[] bytes = new byte[]{(byte) 0x55,(byte) 0x08,(byte) 0x68, (byte) 0x14, 
                          (byte) 0x93, (byte) 0x01, (byte) 0x00, (byte) 0x00, 
                          (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, 
                          (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, 
                          (byte) 0x13, (byte) 0x50, (byte) 0x00, (byte) 0x00, 
                          (byte) 0x00, (byte) 0x22, (byte) 0x09, (byte) 0x11};

0x55 is the title. As stated in the documentation, it will be excluded.

Whenever I try to use this array in java (with 0xbe, 0xd9), RFID works. My problem is generating these checksum values. I searched for almost the entire network, but no chance. I could not find an algorithm that creates 0xbe, 0xd9.

Any idea for me is very welcome. Thanks in advance.

edit: here is the protocol that is provided with rfid

+5
source share
3 answers

I'm not really sure if this is the correct Java translation of the C crc16 algorithm .... but it shows the correct result for your example!

Before using it, compare other results with Mac CRC16 and check it .

public class Crc16 {
public static void main(String... a) {
    byte[] bytes = new byte[] { (byte) 0x08, (byte) 0x68, (byte) 0x14, (byte) 0x93, (byte) 0x01, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x01,
            (byte) 0x00, (byte) 0x13, (byte) 0x50, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x22, (byte) 0x09,
            (byte) 0x11 };
    byte[] byteStr = new byte[4];
    Integer crcRes = new Crc16().calculate_crc(bytes);
    System.out.println(Integer.toHexString(crcRes));

    byteStr[0] = (byte) ((crcRes & 0x000000ff));
    byteStr[1] = (byte) ((crcRes & 0x0000ff00) >>> 8);

    System.out.printf("%02X\n%02X", byteStr[0],byteStr[1]);
} 

int calculate_crc(byte[] bytes) {
    int i;
    int crc_value = 0;
    for (int len = 0; len < bytes.length; len++) {
        for (i = 0x80; i != 0; i >>= 1) {
            if ((crc_value & 0x8000) != 0) {
                crc_value = (crc_value << 1) ^ 0x8005;
            } else {
                crc_value = crc_value << 1;
            }
            if ((bytes[len] & i) != 0) {
                crc_value ^= 0x8005;
            }
        }
    }
    return crc_value;
}

}
+2
source

Perhaps you are looking for this?

Crc16 in java

I use the same array (table variable) in this method:

 public Integer computeCrc16(byte[] data) {
    int crc = 0x0000;
    for (byte b : data) {
        crc = (crc >>> 8) ^ table[((crc ^ b) & 0xff)];

    }

    return (int) crc;
}
0
source

An implementation of CRC16 already exists in the Java ( rt.jar) runtime .

Refer to grepcode for source.

You might be able to see it in your IDE if you are looking for CRC16:

class search image for CRC16

0
source

All Articles