How is the Checksum from Hex file calculated?
You add the byte values, like you've done. From that sum you take only the least significant byte.
Then for Motorola HEX (SREC): Then you take the one's complement of that byte by inverting its bits (i.e. 1s turns to 0s and vice versa).
Then for Intel HEX: Then you take the two's complement of that byte by inverting its bits (i.e. 1s turns to 0s and vice versa) and then you add 1.
Going by your example you have the sum 0x555. Then take the least significant byte, which is 0x55.
For Motorola HEX (SREC): Calculate the one's complement of that. You get 0xAA as the checksum.
For Intel HEX: Calculate the two's complement of that. You get 0xAB as the checksum.
Related videos on Youtube
chillydk147
Updated on September 14, 2022Comments
-
chillydk147 over 1 year
as the title says I'm curious to know how the checksum value is calculated, from what I've read it calculated using 2s complement. Below is a 2 lines from the hex file which was loaded onto my Microcontroller, I've added spaces to make it easier to read, S315 appears on every line, the address on line 1 is 080C0000 followed by 16 hex values which represent the bytes, the values AA on line 1 and AB on line 2 are I assume the checksum values.
For line 1 I've tried adding the following
15+08+0C+00+00+4D+53+53+70+6F+74+31+00+66+10+AE+19+7E+63+1F+78
which gives me555
Hex or010101010101
in binary. I've entered the binary value into an online 2s complement calculator but it always says "invalid binary"??S3 15 080C0000 4D 53 53 70 6F 74 31 00 66 10 AE 19 7E 63 1F 78 AA
S3 15 080C0010 00 00 00 00 45 85 63 EB FF FF FF FF 04 00 03 00 AB
-
jksoegaard over 8 yearsIt is not specific for a single microcontroller. He's talking about the Intel HEX format, which is a standard used by many vendors for years (decades probably).
-
-
chillydk147 over 8 yearsOkay, 0x55 is 0xAA inverted? I didn't need to subtract 1 from it
-
jksoegaard over 8 yearsPerhaps I was a bit unclear - if you take the twos-complement of 0x55, you get 0xAB. Then you need to subtract 1 to get 0xAA.
-
chillydk147 over 8 years0x55 is 01010101, and the inverted bits will be 10101010 or 0xAA??