java crc8_如何用java实现CRC8验证算法

展开全部

/*http://www.koders.com/java/fid1EAF386550BCF07C458CEE6FD966D257A3337235.aspx?s=Address#L34

*---------------------------------------------------------------------------

* Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved.

*

* Permission is hereby granted, free of charge, to any person obtaining a

* copy of this software and associated documentation files (the "Software"),

* to deal in the Software without restriction, including without limitation

* the rights to use, copy, modify, merge, publish, distribute, sublicense,

* and/or sell copies of the Software, and to permit persons to whom the

* Software is furnished to do so, subject to the following conditions:

*

* The above copyright notice and this permission notice shall be included

* in all copies or substantial portions of the Software.

*

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

* IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES

* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

* OTHER DEALINGS IN THE SOFTWARE.

*

* Except as contained in this notice, the name of Dallas Semiconductor

* shall not be used except as stated in the Dallas Semiconductor

* Branding Policy.

*---------------------------------------------------------------------------

*/

package com.dalsemi.onewire.utils;

/**

* CRC8 is a class to contain an implementation of the

* Cyclic-Redundency-Check CRC8 for the iButton. The CRC8 is used

* in the 1-Wire Network address of all iButtons and 1-Wire

* devices.

*

* CRC8 is based on the polynomial = X^32313133353236313431303231363533e58685e5aeb9313332646432338 + X^5 + X^4 + 1.

*

* @version 0.00, 28 Aug 2000

* @author DS

*

*/

public class CRC8

{

//--------

//-------- Variables

//--------

/**

* CRC 8 lookup table

*/

private static byte dscrc_table [];

/*

* Create the lookup table

*/

static

{

//Translated from the assembly code in iButton Standards, page 129.

dscrc_table = new byte [256];

int acc;

int crc;

for (int i = 0; i < 256; i++)

{

acc = i;

crc = 0;

for (int j = 0; j < 8; j++)

{

if (((acc ^ crc) & 0x01) == 0x01)

{

crc = ((crc ^ 0x18) >> 1) | 0x80;

}

else

crc = crc >> 1;

acc = acc >> 1;

}

dscrc_table [i] = ( byte ) crc;

}

}

//--------

//-------- Constructor

//--------

/**

* Private constructor to prevent instantiation.

*/

private CRC8 ()

{

}

//--------

//-------- Methods

//--------

/**

* Perform the CRC8 on the data element based on the provided seed.

*

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc data element on which to perform the CRC8

* @param seed seed the CRC8 with this value

* @return CRC8 value

*/

public static int compute (int dataToCRC, int seed)

{

return (dscrc_table [(seed ^ dataToCRC) & 0x0FF] & 0x0FF);

}

/**

* Perform the CRC8 on the data element based on a zero seed.

*

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc data element on which to perform the CRC8

* @return CRC8 value

*/

public static int compute (int dataToCRC)

{

return (dscrc_table [dataToCRC & 0x0FF] & 0x0FF);

}

/**

* Perform the CRC8 on an array of data elements based on a

* zero seed.

*

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [])

{

return compute(dataToCrc, 0, dataToCrc.length);

}

/**

* Perform the CRC8 on an array of data elements based on a

* zero seed.

*

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param off offset into array

* @param len length of data to crc

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int off, int len)

{

return compute(dataToCrc, off, len, 0);

}

/**

* Perform the CRC8 on an array of data elements based on the

* provided seed.

*

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param off offset into array

* @param len length of data to crc

* @param seed seed to use for CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int off, int len, int seed)

{

// loop to do the crc on each data element

int CRC8 = seed;

for (int i = 0; i < len; i++)

CRC8 = dscrc_table [(CRC8 ^ dataToCrc [i + off]) & 0x0FF];

return (CRC8 & 0x0FF);

}

/**

* Perform the CRC8 on an array of data elements based on the

* provided seed.

*

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param seed seed to use for CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int seed)

{

return compute(dataToCrc, 0, dataToCrc.length, seed);

}

}

追问

seed参数的作用是什么?

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值