Shop OBEX P1 Docs P2 Docs Learn Events
Checksum math, how the hex do I do this — Parallax Forums

Checksum math, how the hex do I do this

jesse214jesse214 Posts: 2
edited 2010-11-13 15:33 in BASIC Stamp
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! ;-)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-13 15:20
    Normally, if you know ahead of time that the next two bytes contain a value expressed as hexadecimal, you can use the HEX2 formatter to automatically convert the two hexadecimal digits as they're received into a single 8 bit byte that you can use to check against your checksum. If you don't know ahead of time, you'll need to do the conversion yourself, one character at a time.

    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.
  • jesse214jesse214 Posts: 2
    edited 2010-11-13 15:33
    Ah! Yes, that makes sense. I will always know what to expect based on the protocol. The external circuit will only send data based on what I send it first, so I'll know the parameters of the response, hence know what the CS is.

    Thanks!!!
Sign In or Register to comment.