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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking 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. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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];
    		}
     
    		//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");
    		}
     
    	}
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. How would one attempt to create an algorithm for this?
    By RedHawkLuffy in forum Java Theory & Questions
    Replies: 2
    Last Post: March 30th, 2013, 01:51 PM
  2. How would I create this solution algorithm using pseudocode?
    By meghere in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 13th, 2013, 10:43 PM
  3. Checksum in UDP datagramSocket
    By cicco.ste90 in forum Java Networking
    Replies: 0
    Last Post: February 7th, 2013, 11:44 AM
  4. Replies: 2
    Last Post: July 5th, 2012, 07:46 PM
  5. Calculating checksum when appending to GZIP stream
    By giorashc in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 4th, 2011, 05:11 AM

Tags for this Thread