Search code examples
hexparity

Decode HEX string


I have this string received by a serial port (HEX):

02 30 31 30 30 3f 50 35 32 0d 0a
02 30 31 30 31 3f 55 35 38 0d 0a
02 30 31 30 32 3f 48 34 43 0d 0a
02 30 31 30 33 3f 45 34 41 0d 0a
02 30 31 30 34 3f 50 35 36 0d 0a
02 30 31 30 35 3f 55 35 43 0d 0a
02 30 31 30 36 3f 48 35 30 0d 0a
02 30 31 30 37 3f 45 34 45 0d 0a
02 30 31 30 38 3f 50 35 41 0d 0a
02 30 31 30 39 3f 55 36 30 0d 0a
02 30 31 31 30 3f 48 34 42 0d 0a
02 30 31 31 31 3f 45 34 39 0d 0a
02 30 31 31 32 3f 50 35 35 0d 0a
02 30 31 31 33 3f 55 35 42 0d 0a
02 30 31 31 34 3f 48 34 46 0d 0a
02 30 31 31 35 3f 45 34 44 0d 0a




1° hex: STX
2° & 3°: fixed
4° & 5°: from 30 30 to 39 39
6°: fixed
7°: same routine 55 / 48 / 45 / 50 
8° & 9°: my question
10°: CR
11°: LF

Someone can help me for the logic behind 8 and 9 hex?

Many thanks Andrea


Solution

  • Add all the bytes from 1 to 7. Then take the last two digits of the result, in hexadecimal (using capital letters). Get the ascii values for each digit, and those are the two bytes of the checksum. For example, adding all bytes from first line gives 0x152. The ascii codes for 5 and 2 are 0x35 and 0x32.