CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Checksum

  1. #1
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Checksum

    Hello!

    I have a 8-char-string for which I have to do a checksum. Which would be the best way to do it? I thought of a kind of a weighted sum like:

    sum = char[0]*1+char[1]*2+char[2]*3+char[3]*4+char[4]*5+char[5]*6+char[6]*7+char[7]*8 % 255;

    But this could not alarm if (for example):
    char[0]: Original: 0 Fault transmitted: 2
    char[1]: Original: 2 Fault transmitted: 0

    Is there a better way?

  2. #2
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275
    The situation that two values have an error that cancels each other is very low.
    Cheksums makes just that, it makes the possibility of error not null, but much lower than without checksum.
    There are some better algorithms, there are algorithms that finds and corrests an error, but I never used them.

  3. #3
    Join Date
    Oct 2002
    Posts
    1,134
    Is it a requirement that you use a checksum? That hardly seems worthwhile for an 8-byte transmission, since most checksum algorithms will require at least 2 bytes. If you really want to be sure the 8 bytes have been correctly received, the easiest thing to do is to keep transmitting them until the receiver gets the same string twice.

    But if the requirement is to use checksumming (for whatever reason), you are correct in that a simple sum such as you propose will fail if there are two (or more) compensating errors. One solution to this would be to consider the 8-byte string as a number, then select some other number - say 11 - and use it to divide the 8-byte number; the remainder would form a one-byte checksum of sorts.

    There are all sorts of methods out there for ensuring that data gets transmitted correctly. Check out this reference for an interesting discussion.
    Regards
    Robert Thompson

  4. #4
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338
    Hi TSYS,
    thanx for your answer. Yes the checksum is needed because the string represents a barcode. So you would prefer a division instead of a sum? I will have a look at the link.

    One last question: I'm not quite sure what a remainder is (sorry, my english...):
    Example: 7/3
    Is the remainder
    1 (% or mod)
    or
    2 (7/3 = 2.33333 is rounded 2).

  5. #5
    Join Date
    Feb 2002
    Posts
    3,788
    if you want to read more about Checksums I can email you quite a lot of docs.

  6. #6
    Join Date
    Oct 2002
    Posts
    1,134
    "Remainder" refers to what's left over after a division, so the remainder of 7/3 (also referred to as REM( 7/3 ) ) would be 1, and the remainder of 7/4 would be 3. The C++ "modulo" operator - % - returns the remainder of the division.

    If you're working with bar codes, isn't the last byte already a checksum? I thought that was how they worked....
    Regards
    Robert Thompson

  7. #7
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338
    Hi Myth, thanks, I'll check TSYS link out and come back to you if needed.

    Hi TSYS: Yes, the barcode itself has a checksum, but it's not very strong and it's only for the barcode-reader. If someone gives in the barcode in a keyboard (for example because the code is damaged), there is no checksum at all.

  8. #8
    Join Date
    May 2012
    Posts
    4

    Re: Checksum

    Hi, you may try a free barcode control online which could help you calculate the checksum automatically with barcode generator for .NET applications

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Checksum

    Quote Originally Posted by beckbutler376 View Post
    Hi, you may try a free barcode control online which could help you calculate the checksum automatically with barcode generator for .NET applications
    Do you really think that martho still needs your spam? After more then 11 years?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured