![]() |
| | #1 |
| Registered User Join Date: Nov 2007
Posts: 69
| checksum sender vs reciever? 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
thank you. |
| silhoutte75 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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. |
| tabstop is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Making of checksum using XOR | Subsonics | C Programming | 4 | 02-20-2009 01:19 PM |
| C# and SQL | siten0308 | C# Programming | 2 | 07-09-2008 12:34 PM |
| weird checksum function | sagitt13 | C Programming | 7 | 10-31-2006 01:25 AM |
| Need help with calculator program | Kate | C# Programming | 1 | 01-16-2004 10:48 AM |