views:

62

answers:

2

I have an array of long values in my c# program. Now I need a checksum (the smaller the better) to see if all values are incuded in an other array I want to compare with.

It should be an easy checksum so I can use it with JavaScript/JQuery too. Is there a method/code snippet I can use?

Background:

I have a javascript ajax mvc project that connects periodically (every 10 seconds) to my c# webservice to get a list of long values and sends the last value it has. The long values are consecutive so I will only send the new ones since the last value the client has. But sometimes (1:1000) there is a new long value that is "older" and not consecutive. To avoid that the javascript will not get this value (as it gets only the newest values) I want to add a checksum to see if it has every value. If the checksum is not correct it should fetch the complete list again.

+1  A: 

the simplest way:

 unchecked
 {
      long checksum = 0;
      for( int i = 0; i < values.Length; i++)
          checksum = checksum ^ values[i];
      return checksum;
 }
STO
*if* it can be known no value will be negative, make your checksum a ulong for safety..
Jimmy Hoffa
The values are positive. Can I also use this with JavaScript? Is there something like unchecked? What is when an overflow happens (it does not matter in c# as it is unchecked, but in JavaScript)?
Chris
A: 

If entries are never deleted from the list, you can use count of all objects in the list as a “checksum”.

But it seems better to me not to base retrieving new entries solely on the number would be better. You can base it on server time, so the server remembers when was an object added and the client request all new objects since x.

svick