Checksumming

Hex to Binary to Decimal

bin hex decimal
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 a 10
1011 b 11
1100 c 12
1101 d 13
1110 e 14
1111 f 15

Converting from binary to decimal:

Starting on the right multiply each binary digit by $2^n$ where n is the position of the bit starting with bit 0 on the right. for example: $1 \cdot 2^0 + 0 \cdot 2^1 + 1 \cdot 2^2 + 1 \cdot 2^3 = 13$

Converting from decimal to binary:

Repeatedly divide by 2, the remainder becomes the next bit building from right to left. For example to convert 13 to binary:

  • 13 /2 = 6 remainder 1
  • 6 / 2 = 3 remainder 0
  • 3 / 2 = 1 remainder 1
  • 1 / 2 = 0 remainder 1

Link to ascii chart

Computing a Checksum

  1. Add the integer value for the first character to the second
  2. If the number is > 255 then you must "lop off" the leftmost bit and add 1 to the result
  3. Continue this process with the rest of the numbers/characters

When all of the letters of the message have been added then take the one's complement (flip all the bits)

Checking a checksum

  1. Repeat the same process (steps 1-3) as we did in Computing a Checksum (but not the one's complement part)
  2. Now add in the included checksum
  3. If all the bits are 1, that is, the sum is 255 then there are no errors!

Exercise

  1. Write a function to take a string and compute the sum of the letters in the string.
  2. Write a function to take the one's complement of that sum.
  3. combine 1 and 2 into a checksum function.
  4. Write a function that checks the checksum.
  5. TEST

  6. Find a team of 3 people. Each of you will send a message (maybe 2 or 3 random words) to another member of your team using the third member as an intermediary as follows:

    1. make up a short message and compute a checksum for that message.
    2. Mail the message and the checksum to team member 2 -- Team member 2 may choose to change one or more letters of the message or the checksum and will send a copy of the (potentially altered) message to team member 3.
    3. Team member three will verify whether the message was altered or not.

Further Investigation

  • can checksums detect characters that are just out of order?
  • What happens if all the bits are one and you have a number larger than 255? Can that even happen?