Opinions on 16-bit checksums.

From: Kermit (Kermit333_at_hotmail.com)
Date: 08/25/04


Date: Tue, 24 Aug 2004 22:17:49 GMT

Hello all,

Since I see that Endianess is a current topic, I'd like to ask a question
concerning it and calculating 16-bit checksums. I've never seen a standard
anywhere that describes exactly what a checksum is.

However, I have mainly seen them calculated in one of three ways.

1) Simply calculate a 32-bit value which is the sum of each byte added to a
running sum. At the end, cast this 32-bit value to a 16-bit value, and call
it the checksum.

2) Calculate the same way as above, but pull two bytes of the file out at a
time such that you are adding them as 16-bit values in one of the following
ways.

        a) Little-endian
        b) Big-endian.

For example, lets say I have a 6-byte file consisting of:

0x00 0x01 0x02 0x03 0x04 0x05

Algorithm 1 would yield:

0x00 + 0x01 + 0x02 + 0x03 + 0x04 + 0x05 = 0x000f

Algorithm 2-a would yield:

0x0100 + 0x0302 + 0x0504 = 0x0906

Algorithm 2-b would yield:

0x0001 + 0x0203 + 0x0405 = 0x0609

I've always preferred to calculate it 16-bits at a time, because that way,
you can detect byte-reversal issues. And, apparently, I've always calculated
them assuming a little endian format. However, I recently ran into a problem
with a distributor trying to program a big-endian file into a flash part.
They switched the bytes, then ignored it, which is their fault, but my
problem. But, since it was the first time I'd ever worked with a big-endian
binary file, I guess it threw me and I wanted to double check my thinking.

How does everyone else on the list calculate a 16-bit checksum.

--kermit

-- 
It wasn't easy being Greazy ....but it was interesting.


Relevant Pages

  • hexadecimal calculating: turning on the high bit
    ... I have hardly any experience with hexadecimal calculating and 'and' ... cHex: string; ... iChecksum: integer; ... I find that the only example that doesnt compute right the checksum ...
    (alt.comp.lang.borland-delphi)
  • Re: How Extract The Fedorecore iso cd
    ... Checksum is calculated before burning the CD to make sure the ... For calculating the sum, follow the instructions here ... the images are good and you can burn them to CDs using Nero. ... If you are downloading the DVD image, the CD images are not required. ...
    (Fedora)
  • Re: ReadFile failed for a file in release directory WM6.1
    ... That will display the file (whose checksum is calculated) and the ... The file actually is a driver dll that I need to read. ... of WM 6.1 that they are calculating the hash for any particular driver ...
    (microsoft.public.windowsce.platbuilder)
  • Re: CRC32 on ELF in C problem
    ... > sections i need to calculate the checksum for. ... > actually calculating the checksum using a provided crc32 algorithm. ... > that I have commented out fread and used fprintf instead. ... That better should return an unsigned int. ...
    (comp.programming)
  • Re: [PATCH] char/moxa.c: fix endianess and multiple-card issues
    ... While testing Moxa C218T/PCI on PowerPC 405EP I found that loading ... firmware using the linux kernel driver fails because calculation of the ... checksum is not endianess independent in the original code. ...
    (Linux-Kernel)