# can't create checksum algorithm/ can't understand concept of checksum

• October 5th, 2013, 11:25 AM
can't create checksum algorithm/ can't understand concept of checksum
Here is my code:

Code Java:

```  int[] a = {1, 2, 3, 4, 5} checksum = 0; int i = 0;   while ( a[i] < a.length-1 ) { checksum += a[i] + a[i - 1] ; i++; }   checksum %= i;```

Another problem is I can't really grasp what a "checksum" is. I've looked it up but don't get it. Could I have a simple example please? I need to fix my code ASAP.
• October 5th, 2013, 12:05 PM
helloworld922
Re: can't create checksum algorithm/ can't understand concept of checksum
There are many different ways to implement a checksum. See: Wikipedia: List of Checksums.
• October 5th, 2013, 12:12 PM
newbie
Re: can't create checksum algorithm/ can't understand concept of checksum
An implementation of a checksum varies, however its goal is always the same: To determine if data has been transported without errors or not.
To show the the basic idea of a checksum, i've written a simple example which uses a very basic "summation" checksum, which computes the checksum based on the sum of all the payload bytes. Via the comments, assume this data array is transferred across a socket or whatever.

Code java:

``` public static void main(String[] args){   //SENDER final byte[] data = new byte[4]; data[0] = 1; data[1] = 2; data[2] = 3;   //calculate checksum for(int i = 0; i < data.length-1; i++){ data[data.length-1] += data[i]; }   //RECEIVER   final byte[] received = data;   //calculate checksum byte checksum = 0; for(int i = 0; i < data.length-1; i++){ checksum += received[i]; }   //CHECKSUM DOESNT MATCH! DATA CORRUPTED! if(checksum != received[4]){ System.out.println("An error occured"); }   }```