Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: None  |  size: 0.59 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. CRC-CCITT validation in java (android)
  2. int crc = 0xFFFF;          // initial value
  3.     int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12)
  4.  
  5.     // byte[] testBytes = "123456789".getBytes("ASCII");
  6.  
  7.     byte[] bytes = args[0].getBytes();
  8.  
  9.     for (byte b : bytes) {
  10.         for (int i = 0; i < 8; i++) {
  11.             boolean bit = ((b   >> (7-i) & 1) == 1);
  12.             boolean c15 = ((crc >> 15    & 1) == 1);
  13.             crc <<= 1;
  14.             if (c15 ^ bit) crc ^= polynomial;
  15.          }
  16.     }
  17.  
  18.     crc &= 0xffff;
  19.     System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));