论坛首页 入门技术论坛

CRC16算法Java实现

浏览 7332 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-01-17  
OO

模仿C++代码改写的Java实现

 

public class CRC16 {
    private short[] crcTable = new short[256];
    private int gPloy = 0x1021; // 生成多项式

    public CRC16() {
        computeCrcTable();
    }

    private short getCrcOfByte(int aByte) {
        int value = aByte << 8;

        for (int count = 7; count >= 0; count--) {
            if ((value & 0x8000) != 0) { // 高第16位为1,可以按位异或
                value = (value << 1) ^ gPloy;
            } else {
                value = value << 1; // 首位为0,左移
            }

        }
        value = value & 0xFFFF; // 取低16位的值
        return (short)value;
    }

    /*
     * 生成0 - 255对应的CRC16校验码
     */
    private void computeCrcTable() {
        for (int i = 0; i < 256; i++) {
            crcTable[i] = getCrcOfByte(i);
        }
    }

    public short getCrc(byte[] data) {
        int crc = 0;
        int length = data.length;
        for (int i = 0; i < length; i++) {
            crc = ((crc & 0xFF) << 8) ^ crcTable[(((crc & 0xFF00) >> 8) ^ data[i]) & 0xFF];
        }
        crc = crc & 0xFFFF;
        return (short)crc;
    }
}

 

public final class CodecUtil {
    static CRC16 crc16 = new CRC16();

    private CodecUtil() {
    }

    public static byte[] short2bytes(short s) {
        byte[] bytes = new byte[2];
        for (int i = 1; i >= 0; i--) {
            bytes[i] = (byte)(s % 256);
            s >>= 8;
        }
        return bytes;
    }

    public static short bytes2short(byte[] bytes) {
        short s = (short)(bytes[1] & 0xFF);
        s |= (bytes[0] << 8) & 0xFF00;
        return s;
    }

    /*
     * 获取crc校验的byte形式
     */
    public static byte[] crc16Bytes(byte[] data) {
        return short2bytes(crc16Short(data));
    }

    /*
     * 获取crc校验的short形式
     */
    public static short crc16Short(byte[] data) {
        return crc16.getCrc(data);
    }

    public static void main(String[] args) {
        byte[] test = new byte[] {0, 1, 2, 3, 4};
        byte[] crc = crc16Bytes(test);

        byte[] testc = new byte[test.length + 2];
        for (int i = 0; i < test.length; i++) {
            testc[i] = test[i];
        }
        testc[test.length] = crc[0];
        testc[test.length + 1] = crc[1];

        System.out.println(crc16Short(testc));
    }
}

   发表时间:2009-01-17  
好像现在JAVA API已经有了吧。 :)
0 请登录后投票
   发表时间:2009-02-03  
sdh5724 写道

好像现在JAVA API已经有了吧。 :)

呵呵,API里的那个不是完整的实现。
0 请登录后投票
   发表时间:2009-11-18  
能帮我把这个改成java的吗

#include<string.h>  /* CRC-8 按位计算实现  */

 unsigned
  int cal_crc(unsigned char *ptr, unsigned char len) {
     unsigned char i;
     unsigned char crc=0;
     while(len--!=0)
        {
          for(i=0x80; i!=0; i/=2)
            {
                  if((crc&0x80)!=0)
                        {crc*=2; crc^=0x1f;}   /* 余式CRC乘以2再求CRC  */
                    else crc*=2;
                 if((*ptr&i)!=0) crc^=0x1f;                /* 再加上本位的CRC */
             }
        ptr++;
        }
  return(crc);
}

int  main()
{
      char *a="410400110012";
      printf("%d\n",cal_crc(a,strlen(a))) ;
      getch();
}
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics