Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
474 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (8)


"barcode" checksum by modulus 10Go to Mike Shkolnik's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
12/30/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet barcode check checksum mod10 modulus 10
Times Scored:
4
Visits:
9504
Uploader: Mike Shkolnik
Company: Scalabium Software
Reference: http://www.scalabium.com/smr/index.htm
 
Question/Problem/Abstract:
How can I calculate a checksum by modulus 10 which is used in barcodes?
Answer:



I want to publish a code for checksum calculation by modulus 10 which is used in the barcodes. I must say that this "mod10" is specifical so readf an article if you're interested.

This algorithm is very popular for UPC barcodes (Universal Product Code), hash code or serial number generation for applications etc...

The basic algorithm:

1. add the values of the digits in the odd positions (1, 3, 5...)
2. multiply this result by 3
3. add the values of the digits in the even positions (2, 4, 6...)
4. sum the results of steps 2 and 3
5. the check digit is the smallest number which, when added to the result in step 4, produces a multiple of 10.

Small example. Assume the source data is 08137919805

1. 0+1+7+1+8+5=22
2. 22*3=66
3. 8+3+9+9+0=29
4. 66+29=95
5. 95+??=100 where ?? is a 5 (our checksum)

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 or optimize this algorithm for own needs.

For example, I modified it and now I use it for any characters (not only digits) in source value.

The original algorithm I used for UPC-barcode validation in the SMReport Designer and the my extended algorithm I use in the serial number
generation as part of the protection schema (in the shareware projects).





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
how to print barcode with delphi
    Adi Huang (Sep 6 2005 7:52AM)

Now, I am looking for source code how to make barcode and to print with paper use quick report with delphi.
Respond

RE: how to print barcode with delphi
mohamamd (Mar 15 2006 9:36AM)

oh my god please help me!
Respond

Different Implementation
    anonymus (Dec 29 2001 12:00AM)

function BarCodeValid (ACode: string): boolean;
var
   I: integer;
   SumOdd, SumEven: integer;
   ADigit, AChecksumDigit: integer;
begin
   SumOdd := 0;
   SumEven := 0;
   for I := 1 to (Length (ACode) - 1) do begin
      ADigit := StrToIntDef (ACode [I], 0);
      if (I MOD 2 = 0) then begin
         SumEven := SumEven + ADigit;
      end else begin
         SumOdd := SumOdd + ADigit;
      end; {if}
   end; {for}
   AChecksumDigit := StrToIntDef (ACode [Length (ACode)], 0);
   Result := ((SumOdd*3 + SumEven + AChecksumDigit) MOD 10 = 0);
end; {--BarCodeValid--}

By the way, your example seems to contain some errors. Step 1 in summing up is probably wrong, and/or your sample barcode is invalid.
Respond

RE: Different Implementation
Dmitri Papichev (Dec 29 2001 12:01AM)

That was me again, not anonymous.
Respond

RE: Different Implementation
Mike Shkolnik (Dec 29 2001 9:16AM)

You included the more optimized code (one looping instead mine two's) but I posted a code which is more "visual" because I wanted to explain details only an the article...

Also where did you found errors in my code?

With best regards, Mike Shkolnik
Respond

RE: RE: Different Implementation
Dmitri Papichev (Dec 29 2001 5:19PM)

I did mention errors in your example, not your code, which I did not check. Below is the exerpt from your article:

"The basic algorithm:
1. add the values of the digits in the odd positions (1, 3, 5...)
...
Small example. Assume the source data is 08137919805
1. 0+1+7+8+5=22"

With your sample bar code, either Step 1 should read "0+1+7+1+8=17", or the description of Step 1 should be adjusted.
Also, I think your algorithm descriptions are not entirely clear, for example, it is unclear whether the checksum digit is included into sum up procedures or not.

Respond

RE: RE: RE: Different Implementation
Mike Shkolnik (Dec 30 2001 11:43AM)

Thanks - I missed '1' in step1. I fixed it.

PS: check digit is not included in my sample
Respond

I would consider renaming the article
    Dmitri Papichev (Dec 28 2001 11:14PM)

It doesn't really tells about modular arithmetics, but rather about the bar code verification. I think the title like "Bar Code Verification (Algorithm)" would be more appropriate.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)