C++代码改成C#
测试数据cc760106021400ddee,算出来是8f
比如有一个SendByte[16],求前15字节的CRC码
unsigned char CRC8_Tab(unsigned char ky)
{
unsigned char ki;
for(ki=0;ki<8;ki++)
{
if(ky&0x80)
{
ky*=2;
ky^=0x31;
}
else
{
ky*=2;
}
}
return ky;
}
unsigned char Get_CRC_Result(uchar * kcr)
{
unsigned char gt1,gt2;
gt2=0;
for(gt1=0;gt1<15;gt1++)
{
gt2=CRC8_Tab((*kcr)^gt2);
kcr++;
}
return gt2;
}
Void main(void)
{
unsigned char SendByte[16];
unsigned char chx,km;
For(chx=1;chx<16;chx++)
{
SendByte[chx]=chx+5;
}
Km= Get_CRC_Result(SendByte);//取CRC码
SendByte[15]=km;
}
该回答引用GPT:
C#代码:
public static byte CRC8_Tab(byte ky)
{
byte ki;
for (ki = 0; ki < 8; ki++)
{
if ((ky & 0x80) == 0x80)
{
ky *= 2;
ky ^= 0x31;
}
else
{
ky *= 2;
}
}
return ky;
}
public static byte Get_CRC_Result(byte[] kcr)
{
byte gt1, gt2;
gt2 = 0;
for (gt1 = 0; gt1 < 15; gt1++)
{
gt2 = CRC8_Tab((kcr[gt1]) ^ gt2);
}
return gt2;
}
public static void Main()
{
byte[] SendByte = new byte[16];
byte chx, km;
for (chx = 1; chx < 16; chx++)
{
SendByte[chx] = (byte)(chx + 5);
}
km = Get_CRC_Result(SendByte);//取CRC码
SendByte[15] = km;
}
如还有疑问,可留言帮助解决。
gt2 = CRC8_Tab((kcr[gt1]) ^ gt2); 这句报错,提示错误 2 参数“1”: 无法从“int”转换为“byte” 错误 1 与“WindowsFormsApplication1.Form1.CRC8_Tab(byte)”最匹配的重载方法具有一些无效参数