Fernando André
Fernando André

Reputation: 1213

Hexadecimal calculation for a checksum

I'm not understanding how this result can be zero. This was presented to me has an example to validate a checksum of a message.

ED(12+01+ED=0)

How can this result be zero?

"1201 is the message" ED is the checksum, my question is more on, how can I determine the checksum?

Thank you for any help.

Best regards, FR

Upvotes: 1

Views: 23470

Answers (3)

aioobe
aioobe

Reputation: 420951

How can this result be zero?

The checksum is presumably represented by a byte.

A byte can store 256 different values, so the calculation is probably done module 256.

Since 0x12 + 0x01 + 0xED = 256, the result becomes 0.

how can I determine the checksum?

The checksum is the specific byte value B that makes the sum of the bytes in the message + B = 0 (modulo 256).

So, as @LanceH says in the comment, to figure out the checksum B, you...

  1. add up the values of the bytes in the message (say it adds up to M)
  2. compute M' = M % 256
  3. Now, the checksum B is computed as 256 - M'.

Upvotes: 8

Mike Dinescu
Mike Dinescu

Reputation: 55720

Well, obviously, when you add up 12 + 01 + ED the result overflows 1 byte, and it's actually the hex number 100. So, if you only take the final byte of 0x0100. you get 0.

Upvotes: 0

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115520

I'm not sure about your checksum details but in base-16 arithmetic (and in base-10):

 base-16       base-10
-----------------------
   12            18
   01             1
+  ED           237
------------------------
  100           256

If your checksum is modulo-256 (16^2), you only keep the last 2 base-16 digits, so you have 00

Upvotes: 2

Related Questions