Home : Barcodes : Determining if a Code 39 barcode has a checksum
Q10237 - HOWTO: Determining if a Code 39 barcode has a checksum

In Code 39 barcodes, there is an optional convention that the last character could be a checksum of the previous characters. There is nothing encoded into the barcode that will determine if the convention is being followed -- it is up to the the writer and reader of the barcode to agree on whether the convention is being followed or not.

If you are in a situation where you have barcodes and you don't know if the last character is a checksum, you can only probabilistically determine if it is using the following procedure.

1. Ensure that the Barcode reader is not enforcing checksums

   ReadOpts opts = new ReadOpts();
   opts.EnforceChecksum = false;

2. Check the last character of Code 39 recognized barcodes against the previous ones to see if it is the correct checksum

This code from the WikiPedia (http://en.wikipedia.org/wiki/Code_39) calculates the Code 39 checksum. Pass in the first len-1 characters from the string and the return value should match the last character of the recognized string:

public string ValidateMod43(string barcode)
{
   int subtotal = 0;
   const string charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"; 
 
   for (int i = 0; i < barcode.Length; i++)
   {
      subtotal += charSet.IndexOf(barcode.Substring(i, 1));
   }
 
   return charSet.Substring(subtotal%43, 1);
}

If the character matches, then there is a high probability that this is a checksum character.  If the character does not match, then there is a high probability that this character is not a checksum. It is not a guarantee because the characters could have been read wrong or the last character could randomly match what the checksum would be -- but if you don't know what convention is being followed, this is the best that you can do.

 
Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
http://en.wikipedia.org/wiki/Code_39
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved