Example #1
0
 private string AppendChecksum(string data)
 {
     // append the checksum if necessary
     if (useChecksum)
     {
         if (Checksum == null)
         {
             Checksum = new Modulo10Checksum();
         }
         data += Checksum.Calculate(data);
     }
     return(data);
 }
Example #2
0
        protected string Validate(string data, int fullLength)
        {
            // check for non digits
            if (!new Regex(@"^\d+$").IsMatch(data))
            {
                throw new BarCodeFormatException("The barcode has non-numeric data.");
            }

            // check the length, it can be full or missing the check digit
            if (data.Length < fullLength - 1 || data.Length > fullLength)
            {
                throw new BarCodeFormatException("Invalid length for barcode.");
            }

            // check or calculate and append the check digit
            if (Checksum == null)
            {
                Checksum = new Modulo10Checksum();
            }
            if (data.Length == fullLength)
            {
                // check if the check digit in the string is right
                string checksum = Checksum.Calculate(data.Substring(0, fullLength - 1));
                if (data[fullLength - 1].ToString() != checksum)
                {
                    throw new BarCodeFormatException("Invalid check digit.");
                }
            }
            else
            {
                // append the check digit
                data += Checksum.Calculate(data);
            }

            return(data);
        }