用C#重写(校验码) [英] Rewrite in C# (Checksum code)
本文介绍了用C#重写(校验码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Heyy伙计们!
请解释我用C#重写代码(校验和代码)
Heyy Folks!
Please explain me that code n rewrite in C# (checksum code)
byte crc8(byte *str, int len)
{
int i, f;
byte data;
byte crc;
crc = 0 ;
while (len--)
{
data = *str++ ;
for = (i = 0; i < 8; i++)
{
f = 1 & (data ^ crc);
crc >>= 1;
data >>= 1 ;
if (f)
{
crc ^= 0x8c;
}
}
}
return crc;
}
推荐答案
请看我对这个问题的评论。
使用数组类型byte []
而不是该字节指针,并调用此参数data
,这是不是字符串。 .NET中的字符串是Unicode字符串,不是字节数组(字符不是字节),根本不是数组。因此,使用数组,而不是使用指针增量,使用循环for(int index = 0; index< data.Length; ++ index)
,然后数组元素将是data [index]
。
其他所有内容都差不多。并且在对运行时间有任何疑问的情况下使用调试器。
祝你好运。
-SA
Please see my comment to the question.
Instead of that byte pointer, use the array typebyte[]
and call this parameterdata
, which is not strings. Strings in .NET are Unicode strings, not arrays of bytes (a character is not a byte), and not arrays at all. So, use the array, and, instead of using the pointer increment, use the cyclefor (int index = 0; index < data.Length; ++index)
, then the array element will bedata[index]
.
Everything else will be pretty much the same. And use the debugger in the case of even a slightest doubt of your runtime.
Good luck.
—SA
有关CRC的介绍,请参阅 http://en.wikipedia.org/wiki / Cyclic_redundancy_check [ ^ ]。
尝试将代码与上面链接中显示的示例相匹配。
干杯
Andi
For an intro on CRC see http://en.wikipedia.org/wiki/Cyclic_redundancy_check[^].
Try to match the code with the example shown in the link above.
Cheers
Andi
这篇关于用C#重写(校验码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文