I have a packet and a checksum function. Before sending out the packet, the checksum field is 0. I calculate the checksum and add the value to the checksum field. At the receiving end, when I use the same checksum function, I get a mismatch because when calculating at the sender end, checksum is initially 0, but at the receiver end the checksum is a different, non zero value.
Work around for this is at the receiver end, make the checksum field equal to 0 and calcuate the checksum but this is not very elegant.
I have been looking for a solution for this but of no avail. The checksum function is pretty standard one and its a black box to me.
How should I fix this?
You should not include the checksum digits in the checksum calculation. Calculate the checksum on all bytes excluding the checksum bytes.
By including the bytes reserved to hold the checksum in the checksum calculation, you are burning a few extra CPU cycles and gaining nothing at all.