求大神将下面的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;
}
展开
 我来答
壁虎小威龙
2013-09-14 · TA获得超过284个赞
知道小有建树答主
回答量:271
采纳率:0%
帮助的人:103万
展开全部
{无符号短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;
}
箭影纷飞
2013-09-14
知道答主
回答量:55
采纳率:0%
帮助的人:25.4万
展开全部
你的代码有没有错误?为什么我没看看到对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;
}
追问
而且在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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
求求357493901
2013-09-14 · TA获得超过250个赞
知道答主
回答量:132
采纳率:0%
帮助的人:98.6万
展开全部
这个看不懂。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式