CRC16 效验算法JAVA版

CRC16的C语言算法:

#define PRESET_VALUE 0xFFFF

#define POLYNOMIAL  0x8408

unsigned int uiCrc16Cal(unsigned char const  * pucY, unsigned char ucX)

{

unsigned char ucI,ucJ;

unsigned short int  uiCrcValue = PRESET_VALUE;

    for(ucI = 0; ucI < ucX; ucI++)

  {

  uiCrcValue = uiCrcValue ^ *(pucY + ucI);

      for(ucJ = 0; ucJ < 8; ucJ++)

      {

  if(uiCrcValue & 0x0001)

    {

    uiCrcValue = (uiCrcValue >> 1) ^ POLYNOMIAL;

    }

  else

    {

    uiCrcValue = (uiCrcValue >> 1);

    }

}

  }

return uiCrcValue;

}

CRC16的java语言算法:

package com.xxcg.platform.common.util;

/**

* 包    名 :com.xxcg.platform.common.util

* 文 件 名 : Test

* 描    述 : TODO

* 作    者 :

* 创建时间 :2019/5/9 0009 18:48

* 版    本 :1.0

*/

public class CRC16 {

  //crc效验

  public  CRC16(){

  }

  /**

    * crc16效验算法

    * @param data

    * @param length  长度

    * @return

    */

  public  static int crc16(byte[] data,int length){

      int  ucI,ucJ;

      int  uiCrcValue =0xFFFF;

      for ( ucI  = 0; ucI  < length; ucI ++) {

        uiCrcValue = uiCrcValue ^ (data[ucI] & 0xff);

        for(ucJ = 0; ucJ < 8; ucJ++){

            if((uiCrcValue & 0x0001)==1){

              uiCrcValue = (uiCrcValue >> 1) ^ 0x8408;

            }else{

              uiCrcValue = (uiCrcValue >> 1);

            }

        }

      }

      return  uiCrcValue;

  }

  /**

    * 效验和处理

    * @param data

    * @return

    */

  public  static int crc16(byte[] data){

      int  ucI,ucJ;

      int  uiCrcValue =0xFFFF;

      for ( ucI  = 0; ucI  < data.length; ucI ++) {

        uiCrcValue = uiCrcValue ^ (data[ucI] & 0xff);

        for(ucJ = 0; ucJ < 8; ucJ++){

            if((uiCrcValue & 0x0001)==1){

              uiCrcValue = (uiCrcValue >> 1) ^ 0x8408;

            }else{

              uiCrcValue = (uiCrcValue >> 1);

            }

        }

      }

      return  uiCrcValue;

  }

  public static byte[] int2ByteArray(int i){

      byte[] result=new byte[4];

      result[0]=(byte)((i >> 24)& 0xFF);

      result[1]=(byte)((i >> 16)& 0xFF);

      result[2]=(byte)((i >> 8)& 0xFF);

      result[3]=(byte)(i & 0xFF);

      return result;

  }

  /**

    * 低位 crc16

    * @param data

    * @param length

    * @return

    */

  public  static  byte  lsbCRC16(byte[] data,int length){

      int c =CRC16.crc16(data,length);

      byte[] bytes =    CRC16.int2ByteArray(c);

      return (byte) (bytes[3] & 0xff);

  }

  /**

    * 获取crc两位。 低位在前,高位在后

    * @param data

    * @param length

    * @return

    */

  public  static  byte[]  getLSBANDMSB(byte[] data,int length){

      int c =CRC16.crc16(data,length);

      byte[] bytes =    CRC16.int2ByteArray(c);

      byte[] bydata = new byte[2];

      bydata[0]=bytes[3];

      bydata[1]=bytes[2];

      return  bydata;

  }

  /**

    * 低位 crc16

    * @param data

    * @param length

    * @return

    */

  public  static  byte  msbCRC16(byte[] data,int length){

      int c =CRC16.crc16(data,length);

      byte[] bytes =    CRC16.int2ByteArray(c);

      return (byte) (bytes[2] & 0xff);

  }

  public static void main(String[] args) {

      byte[] data =  new byte[] { 0x05, 0x00, 0x40, 0x01,  (byte) 0x99,0x3A};

      byte c =CRC16.lsbCRC16(data,4);

      System.out.println(c & 0xff);

  }

}


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近公司在做pos业务,以前直接是pos接我们后台c,后台c去对接支付pos支付通道,现在打算用android系统...
    华先生_a70a阅读 9,070评论 6 5
  • 1. ASCII 编码 ASCII(American Standard Code for Information ...
    s酸菜阅读 12,802评论 0 8
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 7,938评论 0 3
  • 蝉声喧闹的小院 渐渐稀少了人烟 你越走越远 回首已是即将来临的秋天 喜欢夏天的夜晚 像梦一样朦胧 像你一样委婉 时...
    诗客阅读 1,526评论 0 0
  • 今天想要分享的一个点是运营的基本功,也是我要不断努力学习和实践的一个点:用户细分。 为什么要做用户细分? 用户细分...
    麻袋在深圳阅读 1,580评论 0 1

友情链接更多精彩内容