Re: Opinions on 16-bit checksums.
From: Hans-Bernhard Broeker (broeker_at_physik.rwth-aachen.de)
Date: 08/25/04
- Next message: David Brown: "Re: Looking for suggestions for processor/module/sbc with ethernet"
- Previous message: Hans-Bernhard Broeker: "Re: Big-Endian vs. Little-Endian"
- In reply to: Kermit: "Opinions on 16-bit checksums."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 25 Aug 2004 07:17:11 GMT
Kermit <Kermit333@hotmail.com> wrote:
> 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.
What made you believe that the community of programmers would ever
have agreed on a single "standard" way of doing it in the first place?
> 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.
I strongly hope nobody actually ever does it that way, on anything
smaller than a 32-bit platform, since that would be quite silly.
Calculating in 32 bits is wasteful if you're only going to be using
the lower 16 bits of the result --- just let a 16-bit sum roll over
and be done with it.
Anyway, this checksumming method is highly dubious, to put it mildly.
There are just too many types of rather likely errors that it won't
ever catch. You don't want a checksum's core operation to be
commutative.
> 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.
Endianness doesn't really enter the figure as an option you would have
to think about. Just interpret pairs of bytes as 16-bit values in
whatever endianness your platform prefers, and add them.
E.g. note that both 2-a and 2-b would write the same checksum, if seen
as bytes:
0x09 0x06
> I've always preferred to calculate it 16-bits at a time, because that way,
> you can detect byte-reversal issues.
But that only makes a difference if reversal of pairs of bytes at at
odd distances (1, 3 or 1023 bytes apart) is more important to detect /
more likely to happen than reversal of pairs an even distance apart
(2, 4, or 1024, e.g.). That assumption seems unwarranted.
-- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.
- Next message: David Brown: "Re: Looking for suggestions for processor/module/sbc with ethernet"
- Previous message: Hans-Bernhard Broeker: "Re: Big-Endian vs. Little-Endian"
- In reply to: Kermit: "Opinions on 16-bit checksums."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|