I am trying to port a piece of code that has been written in C language into C#. The purpose of the code is to calculate the CRC of an array of bytes. But so far I failed. My byte array looks something like this:
data = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00 };
And the C code I am trying to port to C# is as follows.
unsigned int crc_val(unsigned char* data_val, unsigned char data_len)
{
int i;
unsigned int crc_value=0ffff;
while(data_len--)
{
crc_value^=*data_ val++;
for(i=0;i<8;i++)
{
if(crc_value&0x0001)
crc_value=(crc_value>> 1)^0xa001;
else
crc_value=crc_value>> 1;
}
}
return (crc_value);
}
Right now I have this:
private void GetCRC(byte[] message, ref byte[] CRC)
{
ushort CRCFull = 0xFFFF;
char CRCLSB;
for (int i = 0; i < (message.Length) - 2; i++)
{
CRCFull = (ushort)(CRCFull ^ message[i]);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1) & 0xFFFF);
if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRC[1] = (byte)((CRCFull >> 8) & 0xFF);
CRC[0] = (byte)(CRCFull & 0xFF);
}
Please, please, please... Teach me how this code is written in C#, I am a quick learner and once I see the C# implementation of the above function, I shall pick it up from there and finish my application. I thank you for your attention and wish you all spend a fabulous day.