crc16 implementation java

28,251

Solution 1

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

Please compare other results with Mac's CRC16 and stress-test it before using 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;
}

}

Solution 2

There is a CRC16 implementation in the Java runtime (rt.jar) already.

Please see grepcode for the source.

You will probably be able to see it in your IDE if you search for CRC16:

picture of class search for CRC16

Share:
28,251
Ali Yucel Akgul
Author by

Ali Yucel Akgul

Updated on July 09, 2022

Comments

  • Ali Yucel Akgul
    Ali Yucel Akgul almost 2 years

    I am having problems with calculating CRC-16 implementation of a byte array in java. Basically I am trying to send bytes to a RFID that starts writing to a tag. I can see the checksum value of array by looking tcpdump command on mac. But my goal is to generate it by myself. Here is my byte array which 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 header. As the documentation says it will be excluded.

    Whenever I try this array on java (with 0xbe,0xd9), RFID works. My problem is the generating of those checksum values. I searched almost entire web but no chance. I couldn't find any algorithm that produces 0xbe,0xd9.

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

    edit: here is the protocol that provided with rfid

  • Ali Yucel Akgul
    Ali Yucel Akgul over 11 years
    Thanks a lot for your reply. I have already tried that, but no chance. It doesn't give me 0xbe 0xd9 bytes.
  • Roberto Mereghetti
    Roberto Mereghetti over 11 years
    I'm reading the documentation. I'll try to implement the algorithm on Appendix A in Java (have you already tried to do it?)
  • Ali Yucel Akgul
    Ali Yucel Akgul over 11 years
    well actually I don't have so much information about c. basically I didn't convert it to java. but I think it is the same implementation as your post. I only copied appendix a's look up table bytes to mine and tried with that. and again no success. I am in the end of a project. to achieve it, thats my last step.
  • Ali Yucel Akgul
    Ali Yucel Akgul over 11 years
    I don't know how to thank you. Greatly appriciated. But I cannot keep myself off of asking, what do you see as the problem between those two?
  • linjiejun
    linjiejun almost 2 years
    I can find this class.But I can not import this class. That's weird