最近在作一个接收数据的程序,用到CRC16校验码。在网上搜了一天没有结果,那位高人手里有这个程序希望能给小弟些指点!不胜感激!用java编的。谢谢!!!

解决方案 »

  1.   

    Below is the syntax highlighted version of CRC16.java from §5.1 Data Representations./*************************************************************************
     *  Compilation:  javac CRC16.java
     *  Execution:    java CRC16 < data.txt
     *  Dependencies: StdIn.java
     *  
     *  Reads in a sequence of bytes and prints out its 16 bit
     *  Cylcic Redundancy Check (CRC).
     *
     *  1 + x + x^5 + x^12 + x^16 is irreducible polynomial.
     *
     *************************************************************************/public class CRC16 {    public static void main(String[] args) { 
          short crc = (short) 0xFFFF;       // initial contents of LFSR      while (!StdIn.isEmpty()) {
             char c = StdIn.readChar();
             for (int i = 0; i < 8; i++) {
                boolean c15 = ((crc >> 15      & 1) == 1);
                boolean bit = ((c   >> (7 - i) & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit) crc ^= 0x1021;   // 0001 0000 0010 0001  (0, 5, 12) 
             }
          }
          System.out.println("CRC16 = " + crc);
          System.out.println("CRC16 = " + Integer.toHexString(crc));
       }}
      

  2.   

    http://www.cs.princeton.edu/introcs/51data/CRC16.java.html