BYTE – CRC16 Recommend Java

I have a problem when calculating the CRC-16 implementation of a byte array in java. Basically I am trying to send the bytes to the RFID that starts to write the tag. I can use the mac Check the tcpdump command to see the checksum value of the array. But my goal is to generate it myself. This is my byte array, and it 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 the documentation says, it will be excluded.

Whenever I try this array on java (using 0xbe, 0xd9) , RFID can work. My problem is to generate those checksum values. I searched almost the entire network, but no chance. I can’t find any algorithm to generate 0xbe, 0xd9.

Any ideas It is my most popular. Thanks in advance.

Edit: here is the protocol provided with rfid

< div class="answer"> I’m not sure if this is the correct translation of Java in the C crc16 algorithm…
but it shows the correct result of your example!

Please compare other results with Mac’s CRC16 and stress test it before use.

public class Crc16 {< br />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 %02X", byteStr[0],byteStr[1]);
}

int calculate_crc(byte[] bytes) {
int i;
int crc_value = 0;
for (int len ​​= 0; 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;
}

}

I have a problem in calculating the CRC-16 implementation of a byte array in java. Basically I am trying to send the bytes to the RFID that starts to write the tag. I can pass in Check the tcpdump command on the mac to see the checksum value of the array. But my goal is to generate it myself. This is my byte array, and it 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 the document says , It will be excluded.

Whenever I try this array on java (using 0xbe, 0xd9), RFID works. My problem is to generate those checksum values. I searched Almost the entire network, but no chance. I can’t find any algorithm to generate 0xbe, 0xd9.

Any ideas are my most popular. Thanks in advance.

Edit: here is the protocol provided with rfid

I’m not sure if this is the correct translation of Java in C crc16 algorithm….
But it shows your The correct result of the example!

Please compare other results with Mac’s CRC16 and stress test it before use.

public class Crc16 {< br />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
%02X", byteStr[0],byteStr[1]);
}

int calculate_crc(byte[] bytes) {
int i;
int crc_value = 0;
for (int len ​​= 0; 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;
}

}

Leave a Comment

Your email address will not be published.