Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

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

Here is my code:

```
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.

2. ## 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.

3. ## 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.

```	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];
}

//calculate checksum
byte checksum = 0;
for(int i = 0; i < data.length-1; i++){