如何用java实现CRC8验证算法
答案:1 悬赏:60 手机版
解决时间 2021-04-16 04:05
- 提问者网友:容嬷嬷拿针来
- 2021-04-15 08:34
如何用java实现CRC8验证算法
最佳答案
- 五星知识达人网友:鸽屿
- 2021-04-15 09:53
package com.dalsemi.onewire.utils;
public class CRC8
{
//--------
//-------- Variables
//--------
private static byte dscrc_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 CRC8 ()
{
}
//--------
//-------- Methods
//--------
public static int compute (int dataToCRC, int seed)
{
return (dscrc_table [(seed ^ dataToCRC) & 0x0FF] & 0x0FF);
}
public static int compute (int dataToCRC)
{
return (dscrc_table [dataToCRC & 0x0FF] & 0x0FF);
}
public static int compute (byte dataToCrc [])
{
return compute(dataToCrc, 0, dataToCrc.length);
}
public static int compute (byte dataToCrc [], int off, int len)
{
return compute(dataToCrc, off, len, 0);
}
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);
}
public static int compute (byte dataToCrc [], int seed)
{
return compute(dataToCrc, 0, dataToCrc.length, seed);
}
}追问seed参数的作用是什么?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯