title: Calculation of CCITT standard CRC with polynomial x^16 + x^12 + x^5 + 1 in Java
body: <p>I need help with calculating of CCITT standard CRC with polynomial x^16 + x^12 + x^5 + 1 (0x1081) in Java. I have tried many examples on the internet but every one of them returns other values than the ones in the example.</p>
  
  <p>For example for this array [0xFC] [05] [11] the result needs to be [27] [56].</p>
  
  <p>Using this code:</p>
  
  <pre><code>public static void main(String[] args) {
          byte[] array = new byte[3];
          array[0] = (byte) 0xFC;
          array[1] = (byte) 0x05;
          array[2] = (byte) 0x11;
  //        array[3] = (byte) 0x00;
  //        array[4] = (byte) 0x00;
  
          System.out.println(Integer.toHexString(crc16(array)));
      }
  
      private static final int POLYNOMIAL = 0x1081;
      private static final int PRESET_VALUE = 0xFFFF;
  
      public static int crc16(byte[] data) {
          int current_crc_value = PRESET_VALUE;
          for (int i = 0; i &lt; data.length; i++) {
              current_crc_value ^= data[i] &amp; 0xFF;
              for (int j = 0; j &lt; 8; j++) {
                  if ((current_crc_value &amp; 1) != 0) {
                      current_crc_value = (current_crc_value &gt;&gt;&gt; 1) ^ POLYNOMIAL;
                  } else {
                      current_crc_value = current_crc_value &gt;&gt;&gt; 1;
                  }
              }
          }
          current_crc_value = ~current_crc_value;
  
          return current_crc_value &amp; 0xFFFF;
      } 
  </code></pre>
  
  <p>I get result <code>FA DE</code> not [27] [56]</p>
  
  <p>Using this code:</p>
  
  <pre><code>public static void main(String[] args) { 
          int crc = 0x0000;         
          int polynomial = 0x1081;   
  
          // byte[] testBytes = "123456789".getBytes("ASCII");
  
  //        byte[] array = args[0].getBytes();
          byte[] array = new byte[3];
          array[0] = (byte) 0xFC;
          array[1] = (byte) 0x05;
          array[2] = (byte) 0x11;
  
          for (byte b : array) {
              for (int i = 0; i &lt; 8; i++) {
                  boolean bit = ((b   &gt;&gt; (7-i) &amp; 1) == 1);
                  boolean c15 = ((crc &gt;&gt; 15    &amp; 1) == 1);
                  crc &lt;&lt;= 1;
                  if (c15 ^ bit) crc ^= polynomial;
               }
          }
  
          crc &amp;= 0xffff;
          System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));
      }
  </code></pre>
  
  <p>I get this <code>CRC16-CCITT = 8dca</code></p>
  
  <p>Using this code:</p>
  
  <pre><code>private final int polynomial = 0x1081;
  
      private int[] table = new int[256];
  
      public int ComputeChecksum(int[] bytes) {
          int crc = 0xffff;
          for (int i = 0; i &lt; bytes.length; ++i) {
              int index = (crc ^ bytes[i]) % 256;
              crc = (crc &gt;&gt; 8) ^ table[index];
          }
          return crc;
      }
  
      public CRC162() {
          int value;
          int temp;
          for (int i = 0; i &lt; table.length; ++i) {
              value = 0;
              temp = i;
              for (byte j = 0; j &lt; 8; ++j) {
                  if (((value ^ temp) &amp; 0x0001) != 0) {
                      value = (value &gt;&gt; 1) ^ polynomial;
                  } else {
                      value &gt;&gt;= 1;
                  }
                  temp &gt;&gt;= 1;
              }
              table[i] = value;
          }
      }
  
      public static void main(String[] args) {
          CRC162 c = new CRC162();
          int[] arr = new int[]{0xFC, 0x05, 0x11};
          System.out.println(Integer.toHexString(c.ComputeChecksum(arr)));
      }
  </code></pre>
  
  <p>I get this <code>521</code></p>
  
  <p>Hope someone can help me. I need this for communication with device using ID003 protocol.</p>
  
  <p>EDIT:
  Using this online calculator at <a href="http://www.lammertbies.nl/comm/info/crc-calculation.html" rel="nofollow">http://www.lammertbies.nl/comm/info/crc-calculation.html</a> for input FC0511 i get 0x2756 right from CRC-CCITT (Kermit).</p>