Scalabium Software |
|
Knowledge for your independence'. | |
Home Delphi and C++Builder tips |
#129: How can I calculate a checksum by modulus 10? |
|
Today I want to publish a code for checksum calculation by modulus 10. This algirithm is very popular for UPC barcodes (Universal Product Code), hash code or serial number generation for applications etc... The basic algorithm:
Small example. Assume the source data is 08137919805
My implementation in the Pascal: function Mod10(const Value: string): Integer; var i, intOdd, intEven: Integer; begin {add all odd seq numbers} intOdd := 0; i := 1; while (i < Length(Value)) do begin Inc(intOdd, StrToIntDef(Value[i], 0)); Inc(i, 2); end; {add all even seq numbers} intEven := 0; i := 2; while (i < Length(Value)) do begin Inc(intEven, StrToIntDef(Value[i], 0)); Inc(i, 2); end; Result := 3*intOdd + intEven; {modulus by 10 to get} Result := Result mod 10; if Result <> 0 then Result := 10 - Result end; You can expand this algorithm for own needs. For example, I modified it and\par now I use it for any
characters (not only digits) in source value.
|
Copyright© 1998-2024, Scalabium
Software. All rights reserved. |