I need to rewrite the code below in c#.
I think the code is written in c but I only understand a few lines.
Can some help me explain the 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;
}
VulpesPosted Jul 29, 2014, 6:28 AM
Anyway, this C program works fine and outputs 22:
You can actually convert this to C# with very little change if you use 'unsafe' code and compile with the /unsafe switch:
This again outputs 22.
However, if you'd prefer to use 'safe' code, you can use this instead - execution speed will probably be a little slower:
The programs are of course computing a 'cyclic redundancy check' which is often used for checksums and the like.
If you need to understand the mathematics of how this works, I'd check out this Wikipedia article and follow the associated links:
http://en.wikipedia.org/wiki/Cyclic_redundancy_check
Tamas SzigetiPosted Jul 29, 2014, 2:34 PM
I really appreciate your help