unsigned short CalculateCRC(char* buffer, int size)
{
unsigned short cword = 0;
unsigned short ch;
int i,j;
for (i = 0; i < size; i++) {
ch = buffer[i] << 8;
for (j = 0; j < 8; j++) {
if ((ch & 0x8000) ^ (cword & 0x8000)) {
cword = (cword <<= 1) ^ 4129;
}
else {
cword <<= 1;
}
ch <<= 1;
}
}
return cword;
}
I converted above code in to C# but it is not working could any one can figure out the problem. I converted above code as follows.
public static int CalculateCheckSum(byte[] strsResponse)
{
short crc = 0;
try
{
short ch;
for (int i = 0; i < strsResponse.Length; i++)
{
ch = (short)(strsResponse[i] << 8);
for (int j = 0; j < 8; j++)
{
bool xorFlag = (ch & 0x8000)==1?false:true;
if (xorFlag)
{
crc = (short)((crc <<= 1) ^ 4129);
}
else
{
crc <<= 1;
}
ch <<= 1;
}
}
}
catch (Exception ex)
{
return crc = 0;
}
return crc;
}
I cant figure out the problem any one can show me the mistakes.