Checksum math, how the hex do I do this
jesse214
Posts: 2
Hi!
I'm working on an application which communicates with an external device. The device has a series of various protocols, which I understand and can execute successfully.
Where I'm having problems is with doing a Checksum. I can accurately calculate the checksum just fine as well, but it's comparing my calculated checksum with the checksum that is received. This is why.....
The protocol sends/receives using ASCII. The checksum is in terms of Hex. So, lets say it sends a checksum of 0xA2 (dec=162), which it sends as two(2) bytes, being "A" and "2". A2(being 0xA2) has a decimal equivalent of 162.
CS1 = "A" or 0x41 (dec 65)
CS2 = "2" or 0x32 (dec 50)
When I then calculate the Checksum based off the received data, I get A2 (Dec 162). So, they're basically the same.... sort of.
Calculated checksum: CS_Calculated = 0xA2 (162)
So this is the problem.....
Since the received checksum is split into two bytes, being "A" and "2", I don't know how to 'combine' that into a single Hex to compare it with my calculated checksum of "A2".
This problem could be easy, or maybe not easily doable, I have no idea. I spent way too much time on it and now seek help from those who might know what they're doing! ;-)
I'm working on an application which communicates with an external device. The device has a series of various protocols, which I understand and can execute successfully.
Where I'm having problems is with doing a Checksum. I can accurately calculate the checksum just fine as well, but it's comparing my calculated checksum with the checksum that is received. This is why.....
The protocol sends/receives using ASCII. The checksum is in terms of Hex. So, lets say it sends a checksum of 0xA2 (dec=162), which it sends as two(2) bytes, being "A" and "2". A2(being 0xA2) has a decimal equivalent of 162.
CS1 = "A" or 0x41 (dec 65)
CS2 = "2" or 0x32 (dec 50)
When I then calculate the Checksum based off the received data, I get A2 (Dec 162). So, they're basically the same.... sort of.
Calculated checksum: CS_Calculated = 0xA2 (162)
So this is the problem.....
Since the received checksum is split into two bytes, being "A" and "2", I don't know how to 'combine' that into a single Hex to compare it with my calculated checksum of "A2".
This problem could be easy, or maybe not easily doable, I have no idea. I spent way too much time on it and now seek help from those who might know what they're doing! ;-)
Comments
For that, you need to check whether the character is within "0" to "9" or within "A" to "F". If "0" to "9", you just subtract "0" from it. If "A" to "F", you subtract "A" and add 10. You do this for the first byte, shift that left 4 bits to get one result, do it for the second byte, and add the two results together.
Thanks!!!