0
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.

ClickRick
  • 1,553
  • 2
  • 17
  • 37
user1522673
  • 113
  • 1
  • 6
  • 4
    Rather than starting an extensive discussion about what 'doesn't work' means (any compile errors, runtime errors, unexpected behaviour, etc), maybe you should look for an existing CRC implementation for C#. I can't imagine that no-one has written this before in C#. – GolezTrol May 05 '14 at 17:41
  • Actually, I think it already exists in .NET. – GolezTrol May 05 '14 at 17:43
  • @GolezTrol Unless it's hiding somewhere unexpected, oddly it doesn't appear to be in .NET. – Dan Lugg May 05 '14 at 17:45
  • possible duplicate of [How do I calculate CRC32 of a string](http://stackoverflow.com/questions/8128/how-do-i-calculate-crc32-of-a-string) – GolezTrol May 05 '14 at 17:46
  • The result are not equal – user1522673 May 05 '14 at 17:46
  • 2
    http://damieng.com/blog/2006/08/08/Calculating_CRC32_in_C_and_NET – GolezTrol May 05 '14 at 17:47
  • 1
    for starters, `short != unsigned short` and those sign bits do make a difference. – H H May 05 '14 at 18:04
  • `bool xorFlag = (ch & 0x8000)==1?false:true;` is not a good translation of the original. That's one mistake for starters. – ClickRick May 05 '14 at 20:20

1 Answers1

0

Try this:

public static int CalculateCheckSum(byte[] strsResponse)
{
    short crc = 0;
    try
    {
        ushort ch;
        for (int i = 0; i < strsResponse.Length; i++)
        {
                ch = (ushort)(strsResponse[i] << 8);
                for (int j = 0; j < 8; j++)
                {
                    bool xorFlag = ((ch & 0x8000) ^ (cword & 0x8000)) == 0 ? false : true;
                    if (xorFlag)
                    {
                        crc = (ushort)((crc <<= 1) ^ 4129);
                    }
                    else
                    {
                        crc <<= 1;
                    }
                    ch <<= 1;
                }
        }
    }
    catch (Exception ex)
    {
        return crc = 0;
    }
    return crc;
}
Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29