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

* 作? ? 者 :

* 創(chuàng)建時間 :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);

? }

}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近公司在做pos業(yè)務(wù),以前直接是pos接我們后臺c,后臺c去對接支付pos支付通道,現(xiàn)在打算用android系統(tǒng)...
    華先生_a70a閱讀 3,856評論 6 5
  • 1. ASCII 編碼 ASCII(American Standard Code for Information ...
    s酸菜閱讀 8,851評論 0 8
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,203評論 0 3
  • 蟬聲喧鬧的小院 漸漸稀少了人煙 你越走越遠(yuǎn) 回首已是即將來臨的秋天 喜歡夏天的夜晚 像夢一樣朦朧 像你一樣委婉 時...
    詩客閱讀 243評論 0 0
  • 今天想要分享的一個點是運營的基本功,也是我要不斷努力學(xué)習(xí)和實踐的一個點:用戶細(xì)分。 為什么要做用戶細(xì)分? 用戶細(xì)分...
    麻袋在深圳閱讀 243評論 0 1

友情鏈接更多精彩內(nèi)容