How to get Good CRC16 from string in java?

<>

This question already has an answer here:

Answers


You are getting a Bad_CRC because you are calculating Bad_CRC by using 0xFFFF. If you want to calculate Good_CRC then replace it with 0x1D0F.

Here is the code snippet:

public static void main(String[] args) { 
    String input = "A";
    Integer crcRes = crc16(input.getBytes());
    System.out.println("Calculated CRC-CCITT: 0x" + Integer.toHexString(crcRes));
}

private static int crc16(final byte[] buffer) {
    /* Note the change here */
    int crc = 0x1D0F;
    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;
}

Output:

Calculated CRC-CCITT: 0x9479

Need Your Help

How to enable horizontal scrolling in FullPage.js?

javascript jquery css responsive-design fullpage.js

I have a GPA Calculator web application fully operational. I intend for it to be responsive, however it seems as if there are a couple of bugs for smart phones and tablets.

Passing parameters between Request Scoped Managed Beans in JSF + EJB 3.1

jsf-2 java-ee-6 ejb-3.1 managed-bean stateful-session-bean

Our problem is a very basic, simple implementation of editing the database using JSF + EJB.