请将CRC16的C语言算法,改造为JAVA语言算法,万分感谢!
CRC16的C语言算法:#definePRESET_VALUE0xFFFF#definePOLYNOMIAL0x8408unsignedintuiCrc16Cal(uns...
CRC16的C语言算法:
#define PRESET_VALUE 0xFFFF
#define POLYNOMIAL 0x8408
unsigned int uiCrc16Cal(unsigned char const * pucY, unsigned char ucX)
{
unsignedchar ucI,ucJ;
unsignedshort 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;
} 展开
#define PRESET_VALUE 0xFFFF
#define POLYNOMIAL 0x8408
unsigned int uiCrc16Cal(unsigned char const * pucY, unsigned char ucX)
{
unsignedchar ucI,ucJ;
unsignedshort 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;
} 展开
1个回答
展开全部
public static int CRC16(byte[] Buf, int Len) {
int CRC;
int i, Temp;
CRC = 0xffff;
for (i = 0; i < Len; i++) {
CRC = CRC ^ byteToInteger(Buf[i]);
// System.out.println(byteToInteger(Buf[i]));
for (Temp = 0; Temp < 8; Temp++) {
if ((CRC & 0x01) == 1)
CRC = (CRC >> 1) ^ 0xA001;
else
CRC = CRC >> 1;
}
}
return CRC;
}
int CRC;
int i, Temp;
CRC = 0xffff;
for (i = 0; i < Len; i++) {
CRC = CRC ^ byteToInteger(Buf[i]);
// System.out.println(byteToInteger(Buf[i]));
for (Temp = 0; Temp < 8; Temp++) {
if ((CRC & 0x01) == 1)
CRC = (CRC >> 1) ^ 0xA001;
else
CRC = CRC >> 1;
}
}
return CRC;
}
追问
byteToInteger这个函数哪儿的?我无法引用,谢谢!
追答
// 保留低8位,转换成int型
public static int byteToInteger(byte b) {
int value;
value = b & 0xff;
return value;
}
对不起,忘了贴了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询