求大神将下面的C++代码翻译成C#,并能测试正确。。。急!!急!!! 10
unsignedshortgetCRCCheckSW(unsignedchar*pBuffer,intlen){unsignedshortcrc=0;unsignedch...
unsigned short getCRCCheckSW(unsigned char* pBuffer, int len)
{
unsigned short crc = 0;
unsigned char t1;
union bcc_type{
unsigned short int integer;
unsigned char byte[2];
} bcc;
int i,j;
bcc.integer=0;
for (i=0;i<len;i++)
{
t1=pBuffer[i];
for (j=0;j<8;j++)
{
if ((bcc.byte[1]^t1)&0x80)
bcc.integer=(bcc.integer<<1)^0x1021; /*1021H is CCITT-CRC */
else
bcc.integer<<=1;
t1<<=1;
}
}
crc = bcc.byte[1] + bcc.byte[0] * 256;
return crc;
} 展开
{
unsigned short crc = 0;
unsigned char t1;
union bcc_type{
unsigned short int integer;
unsigned char byte[2];
} bcc;
int i,j;
bcc.integer=0;
for (i=0;i<len;i++)
{
t1=pBuffer[i];
for (j=0;j<8;j++)
{
if ((bcc.byte[1]^t1)&0x80)
bcc.integer=(bcc.integer<<1)^0x1021; /*1021H is CCITT-CRC */
else
bcc.integer<<=1;
t1<<=1;
}
}
crc = bcc.byte[1] + bcc.byte[0] * 256;
return crc;
} 展开
3个回答
展开全部
{无符号短getCRCCheckSW(unsigned char * pBuffer,int len)
无符号短crc = 0;
unsigned char t1;
联盟bcc类型{
无符号短int整数;
unsigned char byte[2];
} bcc;
int i,j;
bcc integer = 0;
(我= 0;我<莱恩,我+ +)
{
t1 = pBuffer[我];
(j = 0;j < 8;j + +)
{
如果((bcc字节[1]^ t1)&0x80)
bcc整数=(bcc整数< < 1)^ 0 x1021;/ * 1021 h是国际电报电话咨询委员会crc * /
别的
bcc整数< < = 1;
t1 < < = 1;
}
}
crc = bcc。 字节[1]+ bcc。字节[0]* 256;
返回crc;
}
无符号短crc = 0;
unsigned char t1;
联盟bcc类型{
无符号短int整数;
unsigned char byte[2];
} bcc;
int i,j;
bcc integer = 0;
(我= 0;我<莱恩,我+ +)
{
t1 = pBuffer[我];
(j = 0;j < 8;j + +)
{
如果((bcc字节[1]^ t1)&0x80)
bcc整数=(bcc整数< < 1)^ 0 x1021;/ * 1021 h是国际电报电话咨询委员会crc * /
别的
bcc整数< < = 1;
t1 < < = 1;
}
}
crc = bcc。 字节[1]+ bcc。字节[0]* 256;
返回crc;
}
展开全部
你的代码有没有错误?为什么我没看看到对bcc.byte的更改?
ushort getCRCCheckSW(string pBuffer, int len)
{
ushort crc = 0;
char t1;
short bcc_int=0;
byte[] bcc_byte=new byte[2];
for (int i=0;i<len;i++)
{
char[] myChars=pBuffer.Split();
t1=myChars[i];
for (int j=0;j<8;j++)
{
if ((bcc_byte[1]^(Convert.ToByte(t1)))&0x80)
bcc_int=(bcc_int<<1)^0x1021; /*1021H is CCITT-CRC */
else
bcc_int<<=1;
t1<<=1;
}
}
crc = bcc_byte[1] + bcc_byte[0] * 256;
return crc;
}
ushort getCRCCheckSW(string pBuffer, int len)
{
ushort crc = 0;
char t1;
short bcc_int=0;
byte[] bcc_byte=new byte[2];
for (int i=0;i<len;i++)
{
char[] myChars=pBuffer.Split();
t1=myChars[i];
for (int j=0;j<8;j++)
{
if ((bcc_byte[1]^(Convert.ToByte(t1)))&0x80)
bcc_int=(bcc_int<<1)^0x1021; /*1021H is CCITT-CRC */
else
bcc_int<<=1;
t1<<=1;
}
}
crc = bcc_byte[1] + bcc_byte[0] * 256;
return crc;
}
追问
而且在c++运行得到的结果与你写的不同
追答
不好意思,之前不了解C++,而且手头没有编程工具。去看了一下union的用法,你试试这样做吧
ushort getCRCCheckSW(string pBuffer, int len)
{
ushort crc = 0;
char t1;
short bcc_int=0;
byte[] bcc_byte=new byte[2];
for (int i=0;i<len;i++)
{
char[] myChars=pBuffer.Split();
t1=myChars[i];
for (int j=0;j<8;j++)
{
if ((bcc_byte[1]^(Convert.ToByte(t1)))&0x80)
bcc_int=(bcc_int<<1)^0x1021; /*1021H is CCITT-CRC */
else
bcc_int<<=1;
t1<<=1;
}
}
bcc_byte=BitConverter.GetBytes(bcc_int);
crc = bcc_byte[1] + bcc_byte[0] * 256;
return crc;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个看不懂。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询