Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;

namespace CRC2
{
	class Program
	{
		public static ushort[] crc_table =
		{
	    	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
	    	0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef
		};
		
		public static ushort CalculateCrc( byte[] data )
		{
		    int i;
		    ushort crc = 0;
		    int len = data.Length;
		
		    for (int j = 0; j < len; j++)
		    {
		        i = ( crc >> 12 ) ^ ( data[j] >> 4 );
		        crc = (ushort) (crc_table[ i & 0x0F ] ^ ( crc << 4 ));
		        i = ( crc >> 12 ) ^ ( data[j] >> 0 );
		        crc = (ushort) (crc_table[ i & 0x0F ] ^ ( crc << 4 ));
		    }
		
		    return crc;
		}
		
		public static void Main(string[] args)
		{
			string a = "1";
			
			byte [] numero = new byte[256];

			for (int i=0;i<a.length;i++)>
			{
				numero[i]=(byte)a[i];
			}
			
			ushort CRC = CalculateCrc(numero);


			Console.Write("CRC: ");
			Console.Write(CRC);
			
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
	}
}
Posted
Updated 3-Feb-15 4:44am
v2
Comments
Matt T Heffron 3-Feb-15 13:18pm    
So what line gives the error?
Have you single stepped the code in the debugger?
Member 11423970 3-Feb-15 14:37pm    
Yes, I debugged it. The problem is in the 'for'. When it starts the second loop, it overflows:

for (int j = 0; j < len; j++)
{
i = ( crc >> 12 ) ^ ( data[j] >> 4 );
crc = (ushort) (crc_table[ i & 0x0F ] ^ ( crc << 4 ));
i = ( crc >> 12 ) ^ ( data[j] >> 0 );
crc = (ushort) (crc_table[ i & 0x0F ] ^ ( crc << 4 ));
}
Herman<T>.Instance 3-Feb-15 16:56pm    
What if the numero byte gets longer then 256 ?
Member 11423970 4-Feb-15 2:30am    
Happens the same. I just create an array of 256 because at the beggining was of 16, and I thought that was the cause of the overflow, but after changing, the problem persisted.
CPallini 6-Feb-15 3:01am    
What algorithm are you using? Could you please post a reference to?

1 solution

The problem is the intermediate values in the second part of the crc calculation:
C#
crc = (ushort) (crc_table[ i & 0x0F ] ^ ( crc << 4 ));

the value of crc << 4 in this line is greater than 0xFFFF so the cast to ushort fails.
Add some additional masking to 16 bits to ensure this behaves correctly.
OR
wrap the interior of the for loop in the unchecked construct.

Aside: This symptom only appears if arithmetic overflow/underflow checking is enabled, which, by default, it is not.
 
Share this answer
 
Comments
Member 11423970 4-Feb-15 2:33am    
I don't get it very well. I know it is something to do with ushort, but I don't know how to do the masking.
Matt T Heffron 4-Feb-15 13:30pm    
Just like the
i & 0x0F
above "masks" out the bits of i other than the low order 4 bits,
to mask a value to 16 bits you just bit-and it with 0xFFFF.
Like:
crc = (ushort) ((crc_table[ i & 0x0F ] ^ ( crc << 4 )) & 0xFFFF);
Member 11423970 5-Feb-15 4:30am    
I hava tried both, to mask and to unchecked. With both I avoid now the overflow, but the result I get it isn't the same as the one in http://www.lammertbies.nl/comm/info/crc-calculation.html web page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900