|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
implementing high-level checksum
what i'm trying to do is to implement a (very) high-level checksum for my TCP client app written in C...
here's the data format for the packets i'm sending out from the client: packet1-1033452719-878475\ what i want to do is to do a sum of the ASCII values of each indivual character (using ord())... so it would go something like this: sum = ord('p') + ord('a') + ord('c') + ... + ord(5) then after getting the checksum, i want to add it at the end of the packet too, like so: packet1-1033452719-878475-checksum\ how would i go about doing this? most importantly is how i can iterate over each character of the character array "packet1-1033452719-878475\" and perform the summation thanks! |
|
#2
|
||||
|
||||
|
How about some code like this:
Code:
unsigned char compute_checksum(unsigned char *buf) {
unsigned char *ch, result = 0;
/* buf is assumed to be null terminated */
for (ch = buf; *ch; ch++)
result += (*ch);
return result;
}
....
unsigned char checksum;
....
....
checksum = compute_checksum(buffer);
/* add checksum to end of buffer */
buffer[len] = checksum;
buffer[len+1] = '\0'; /* buffer assumed to be at least len+1 chars */
Please note that I typed this code off the top of my head, so it may have a few syntax errors. This isn't even a very good checksum algorithm, it just adds the ascii value of the chars up. You'd probably want to do a standard 16 or 32 bit CRC algorithm to ensure more reliability. You can probably google for those Hope this helps! |
|
#3
|
||||
|
||||
|
that was fast... thanks man!
![]() it looks good and now i'm gonna see if it's worthwhile to implement a checksum... i just realised that if i do the checksum computation, it may reduce my packet sending rate which i'm trying to get to be as accurate as possible thanks again! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > implementing high-level checksum |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|