Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VB to C#
Message
From
17/04/2009 14:21:12
 
 
To
17/04/2009 13:57:51
General information
Forum:
ASP.NET
Category:
Forms
Title:
Miscellaneous
Thread ID:
01395221
Message ID:
01395324
Views:
50
>>Thanks for noticing. I had already tried with a zero offset with no luck. If it helps here is the original C code provided by the manufacturer.
>>
>>//
>>// CRC Calculation & Checking routines for Kingfisher Protocol
>>//
>>unsigned int kf_crc_calc(BYTE *string, int charnum)
>>{
>>int i, j;
>>int c, crc = 0;
>>for (i=0; i<charnum; i++)
>>{ c = string[i] & 0xff;
>>for (j=0; j<8; j++)
>>{ if (crc & 0x8000)
>>{ crc <<= 1;
>>crc += (((c <<= 1) & 0x100) != 0);
>>crc ^= 0x1021;
>>}
>>else
>>{ crc <<= 1;
>>crc += (((c <<= 1) & 0x100) != 0);
>>}
>>}
>>}
>>return(crc);
>>}
>>int kf_crc_check(BYTE *string, int charnum)
>>{
>>unsigned int crc1 = kf_crc_calc(string, charnum);
>>unsigned int crc2 = (int)string[charnum] * 256 + (int)string[charnum+1];
>>if (crc1 == crc2) return(0);
>>else return(1);
>>}
>>void kf_crc_append(BYTE *string, int charnum)
>>{
>>unsigned int crc = kf_crc_calc(string, charnum);
>>string[charnum] = crc / 256;
>>string[charnum + 1] = crc % 256;
>>}
>>
>
>
>Jim,
>
>I have converted kf_crc_calc()
>
>I cannot really test it since I have no sample (string, output expected)
>
>
>	class test2
>	{
>		static void Main()
>		{
>		}
>
>		static uint kf_crc_calc(string s, int charnum)
>		{
>			int i, j;
>			uint c, crc = 0;
>
>			for (i = -1; ++i < charnum; )
>			{
>				c = (uint)s[i] & 0xffu;
>
>				for (j = 0; j < 8; j++)
>				{
>					c <<= 1;
>
>					if ((crc & 0x8000u) != 0)
>					{
>						crc <<= 1;
>						if ((c & 0x100u) > 0)
>							crc++;
>						crc ^= 0x1021u;
>					}
>					else
>					{
>						crc <<= 1;
>						if ((c & 0x100u) > 0)
>							crc++;
>					}
>
>				}
>			}
>
>			return crc;
>
>		}
>	}
>
Thanks Gregory,

the string 00 08 FF 00 01 22 should return the crc AB 7C . When I put this string into the VB method the CRC AB 7C is returned, but when I put it in the C# method I get back garbage. I think it must have something to do with the way the string is processed. Do you know if the primative data types are the same in VB as in C#? When I run the string through the C# method I get back a very large number even though it should only be 2 bytes.
Previous
Reply
Map
View

Click here to load this message in the networking platform