2

How do I transform the following code into C#. My C is a little bit rusty:

short CalcCrc(char *str,int len)
{
  short crc=0;
  for(int i=0;i<len;i++)
    crc ^= (short)((unsigned char)str[i]) << (i%9);
  return crc;
}
6
  • Many will refuse to do your homework, as will I. Commented Aug 6, 2010 at 9:23
  • @Marcel Gheorghita: It's not homework, I'm helping a friend and I'm not sure on casting (diferent data types c and c#) Commented Aug 6, 2010 at 9:27
  • You have the same operators in C#. Short is short. Unsigned char is byte. char* is string. Commented Aug 6, 2010 at 9:34
  • @Marcel Gheorghita: I'm thinking like this. I'm missing something? crc ^= Convert.ToInt16(Convert.ToByte(str[i]) << (i % 9)); Commented Aug 6, 2010 at 9:44
  • 1
    It seems fine. Are you receiving different results? Commented Aug 6, 2010 at 9:50

1 Answer 1

2
    short CalcCrc(string str)
    {
        short crc = 0;
        for (int i = 0; i < str.Length; i++)
            crc ^= (short)(str[i] << (i % 9));
        return crc;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.