Thread: checksum sender vs reciever?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    checksum sender vs reciever?

    Have the written this one program & unsure of how to reverse it so that it will have a sum check for the reciever & then sum check for sender and compare the 2 sum that will return zero.

    Code:
    /*	Demonstrate the calculation of a checksum using one's
    	complement arithmetic.
    	   Written by:
    	   Date:
    */
    #include <stdio.h>
    #include <string.h>
    #include <pstdint.h>
    
    int main (void)
    {
    //	Local Declarations
    	uint32_t sum      = 0x00000000;
    	uint16_t checksum = 0x0000; 
    
    	char*    str      = "ABCDEFGHI";
    	int      len;
    	int i = 0;
    
    //	Statements
    	len = strlen (str);
    	if (len % 2  == 1)
    	   // Make the number of characters even
    	   len++;
    	for (i = 0; i < len; i += 2)
    	    sum = (sum + str[i] * 256 + str[i + 1]);
    	
    	// Add carries into lower 16 bits
    	while (sum >> 16)
    	   sum = (sum & 0xffff) + (sum >> 16);
    
    	// Complement
    	checksum = ~sum;
    
    	printf ("str:      %s\n",   str);
    	printf ("checksum: %#06X\n", checksum);
    	system("Pause");
    
    	return 0;
    }	// main
    
    /*	Results:
    str:      ABCDEFGHI
    checksum: 0XA5EA
    Please help.
    thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want a comparison that will return 0 when the two numbers are equal, just use subtraction. I don't know what you're asking with the first part: presumably the sender will calculate the checksum and send it, the receiver will calculate the checksum of what was received and will compare that with the received checksum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making of checksum using XOR
    By Subsonics in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 01:19 PM
  2. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  3. weird checksum function
    By sagitt13 in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 01:25 AM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM