Example #1
0
        }//init_CountryCodes

        private void CheckDigit()
        {
            try
            {
                var rawDataHolder = Raw_Data.Substring(0, 11);

                //calculate check digit
                var sum = 0;

                for (var i = 0; i < rawDataHolder.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        sum += Int32.Parse(rawDataHolder.Substring(i, 1)) * 3;
                    }
                    else
                    {
                        sum += Int32.Parse(rawDataHolder.Substring(i, 1));
                    }
                }//for

                int cs = (10 - sum % 10) % 10;

                //replace checksum if provided by the user and replace with the calculated checksum
                Raw_Data = rawDataHolder + cs;
            }//try
            catch
            {
                Error("EUPCA-4: Error calculating check digit.");
            } //catch
        }     //CheckDigit
Example #2
0
        private void CheckDigit()
        {
            //calculate and add check digit if necessary
            if (Raw_Data.Length == 12)
            {
                var even = 0;
                var odd  = 0;

                //odd
                for (var i = 0; i <= 10; i += 2)
                {
                    odd += int.Parse(Raw_Data.Substring(i, 1));
                }

                //even
                for (var i = 1; i <= 11; i += 2)
                {
                    even += int.Parse(Raw_Data.Substring(i, 1)) * 3;
                }

                var total = even + odd;
                var cs    = total % 10;
                cs = 10 - cs;
                if (cs == 10)
                {
                    cs = 0;
                }

                Raw_Data += cs.ToString();
            }
        }
Example #3
0
        }//Encode_EAN8

        private void CheckDigit()
        {
            //calculate the checksum digit if necessary
            if (Raw_Data.Length == 7)
            {
                //calculate the checksum digit
                int even = 0;
                int odd  = 0;

                //odd
                for (int i = 0; i <= 6; i += 2)
                {
                    odd += Int32.Parse(Raw_Data.Substring(i, 1)) * 3;
                }//for

                //even
                for (int i = 1; i <= 5; i += 2)
                {
                    even += Int32.Parse(Raw_Data.Substring(i, 1));
                }//for

                int total    = even + odd;
                int checksum = total % 10;
                checksum = 10 - checksum;
                if (checksum == 10)
                {
                    checksum = 0;
                }

                //add the checksum to the end of the
                Raw_Data += checksum.ToString();
            }//if
        }
Example #4
0
        }//Encode_ITF14

        private void CheckDigit()
        {
            //calculate and include checksum if it is necessary
            if (Raw_Data.Length == 13)
            {
                int even = 0;
                int odd  = 0;

                //odd
                for (int i = 0; i <= 10; i += 2)
                {
                    odd += Int32.Parse(Raw_Data.Substring(i, 1));
                }//for

                //even
                for (int i = 1; i <= 11; i += 2)
                {
                    even += Int32.Parse(Raw_Data.Substring(i, 1)) * 3;
                }//for

                int total = even + odd;
                int cs    = total % 10;
                cs = 10 - cs;
                if (cs == 10)
                {
                    cs = 0;
                }

                this.Raw_Data += cs.ToString();
            }//if
        }
Example #5
0
        private void CheckDigit()
        {
            if (Raw_Data.Length == 7)
            {
                int even = 0;
                int odd  = 0;

                for (int i = 0; i <= 6; i += 2)
                {
                    odd += Int32.Parse(Raw_Data.Substring(i, 1)) * 3;
                }

                for (int i = 1; i <= 5; i += 2)
                {
                    even += Int32.Parse(Raw_Data.Substring(i, 1));
                }

                int total    = even + odd;
                int checksum = total % 10;
                checksum = 10 - checksum;
                if (checksum == 10)
                {
                    checksum = 0;
                }

                Raw_Data += checksum.ToString();
            }
        }
        private void CheckDigit()
        {
            try
            {
                string RawDataHolder = Raw_Data.Substring(0, 11);

                //calculate check digit
                int even = 0;
                int odd = 0;

                for (int i = 0; i < RawDataHolder.Length; i++)
                {
                    if (i % 2 == 0)
                        odd += Int32.Parse(RawDataHolder.Substring(i, 1)) * 3;
                    else
                        even += Int32.Parse(RawDataHolder.Substring(i, 1));
                }//for

                int total = even + odd;
                int cs = total % 10;
                cs = 10 - cs;
                if (cs == 10)
                    cs = 0;

                Raw_Data = RawDataHolder + cs.ToString()[0];
            }//try
            catch
            {
                Error("EUPCA-4: Error calculating check digit.");
            }//catch
        }
        private string Encode_UPCSupplemental_5()
        {
            if (Raw_Data.Length != 5)
            {
                Error("EUPC-SUP5-1: Invalid data length. (Length = 5 required)");
            }

            if (!CheckNumericOnly(Raw_Data))
            {
                Error("EUPCA-2: Numeric Data Only");
            }

            int even = 0;
            int odd  = 0;

            for (int i = 0; i <= 4; i += 2)
            {
                odd += Int32.Parse(Raw_Data.Substring(i, 1)) * 3;
            }

            for (int i = 1; i < 4; i += 2)
            {
                even += Int32.Parse(Raw_Data.Substring(i, 1)) * 9;
            }

            int total = even + odd;
            int cs    = total % 10;

            string pattern = UPC_SUPP_5[cs];

            string result = "";

            int pos = 0;

            foreach (char c in pattern)
            {
                if (pos == 0)
                {
                    result += "1011";
                }
                else
                {
                    result += "01";
                }

                if (c == 'a')
                {
                    result += EAN_CodeA[Int32.Parse(Raw_Data[pos].ToString())];
                }
                else if (c == 'b')
                {
                    result += EAN_CodeB[Int32.Parse(Raw_Data[pos].ToString())];
                }
                pos++;
            }
            return(result);
        }
        /// <summary>
        /// Encode the raw data using the UPC-A algorithm.
        /// </summary>
        private string Encode_UPCA()
        {
            //check length of input
            if (Raw_Data.Length != 11 && Raw_Data.Length != 12)
                Error("EUPCA-1: Data length invalid. (Length must be 11 or 12)");

            if (!CheckNumericOnly(Raw_Data))
                Error("EUPCA-2: Numeric Data Only");

            CheckDigit();

            string result = "101"; //start with guard bars

            //first number
            result += UPC_CodeA[Int32.Parse(Raw_Data[0].ToString())];

            //second (group) of numbers
            int pos = 0;
            while (pos < 5)
            {
                result += UPC_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())];
                pos++;
            }//while

            //add divider bars
            result += "01010";

            //third (group) of numbers
            pos = 0;
            while (pos < 5)
            {
                result += UPC_CodeB[Int32.Parse(Raw_Data[(pos++) + 6].ToString())];
            }//while

            //forth
            result += UPC_CodeB[Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString())];

            //add ending guard bars
            result += "101";

            //get the manufacturer assigning country
            this.init_CountryCodes();
            string twodigitCode = "0" + Raw_Data.Substring(0, 1);
            try
            {
                _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString();
            }//try
            catch
            {
                Error("EUPCA-3: Country assigning manufacturer code not found.");
            }//catch
            finally { CountryCodes.Clear(); }

            return result;
        }
Example #9
0
        private string Encode_UPCA()
        {
            if (Raw_Data.Length != 11 && Raw_Data.Length != 12)
            {
                Error("EUPCA-1: Data length invalid. (Length must be 11 or 12)");
            }

            if (!CheckNumericOnly(Raw_Data))
            {
                Error("EUPCA-2: Numeric Data Only");
            }

            CheckDigit();

            string result = "101";

            result += UPC_CodeA[Int32.Parse(Raw_Data[0].ToString())];

            int pos = 0;

            while (pos < 5)
            {
                result += UPC_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())];
                pos++;
            }

            result += "01010";

            pos = 0;
            while (pos < 5)
            {
                result += UPC_CodeB[Int32.Parse(Raw_Data[(pos++) + 6].ToString())];
            }

            result += UPC_CodeB[Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString())];

            result += "101";

            this.init_CountryCodes();
            string twodigitCode = "0" + Raw_Data.Substring(0, 1);

            try
            {
                _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString();
            }
            catch
            {
                Error("EUPCA-3: Country assigning manufacturer code not found.");
            }
            finally { CountryCodes.Clear(); }

            return(result);
        }
Example #10
0
        private void CheckDigit()
        {
            if (Raw_Data.Length == 13)
            {
                int total = 0;

                for (int i = 0; i <= Raw_Data.Length - 1; i++)
                {
                    int temp = Int32.Parse(Raw_Data.Substring(i, 1));
                    total += temp * ((i == 0 || i % 2 == 0) ? 3 : 1);
                }

                int cs = total % 10;
                cs = 10 - cs;
                if (cs == 10)
                {
                    cs = 0;
                }

                this.Raw_Data += cs.ToString();
            }
        }
Example #11
0
        }//Encode_ITF14

        private void CheckDigit()
        {
            //calculate and include checksum if it is necessary
            if (Raw_Data.Length == 13)
            {
                int total = 0;

                for (int i = 0; i <= Raw_Data.Length - 1; i++)
                {
                    int temp = Int32.Parse(Raw_Data.Substring(i, 1));
                    total += temp * ((i == 0 || i % 2 == 0) ? 3 : 1);
                }//for

                int cs = total % 10;
                cs = 10 - cs;
                if (cs == 10)
                {
                    cs = 0;
                }

                this.Raw_Data += cs.ToString();
            }//if
        }
Example #12
0
        private void CheckDigit()
        {
            try
            {
                string RawDataHolder = Raw_Data.Substring(0, 12);

                int even = 0;
                int odd  = 0;

                for (int i = 0; i < RawDataHolder.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        odd += Int32.Parse(RawDataHolder.Substring(i, 1));
                    }
                    else
                    {
                        even += Int32.Parse(RawDataHolder.Substring(i, 1)) * 3;
                    }
                }

                int total = even + odd;
                int cs    = total % 10;
                cs = 10 - cs;
                if (cs == 10)
                {
                    cs = 0;
                }

                Raw_Data = RawDataHolder + cs.ToString()[0];
            }
            catch
            {
                Error("EEAN13-4: Error calculating check digit.");
            }
        }
Example #13
0
        }//init_CountryCodes

        private void CheckDigit()
        {
            try
            {
                var rawDataHolder = Raw_Data.Substring(0, 12);

                var even = 0;
                var odd  = 0;

                for (var i = 0; i < rawDataHolder.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        odd += Int32.Parse(rawDataHolder.Substring(i, 1));
                    }
                    else
                    {
                        even += Int32.Parse(rawDataHolder.Substring(i, 1)) * 3;
                    }
                }//for

                var total = even + odd;
                var cs    = total % 10;
                cs = 10 - cs;
                if (cs == 10)
                {
                    cs = 0;
                }

                Raw_Data = rawDataHolder + cs.ToString()[0];
            }//try
            catch
            {
                Error("EEAN13-4: Error calculating check digit.");
            }//catch
        }
Example #14
0
        /// <summary>
        /// Encode the raw data using the EAN-13 algorithm. (Can include the checksum already.  If it doesnt exist in the data then it will calculate it for you.  Accepted data lengths are 12 + 1 checksum or just the 12 data digits)
        /// </summary>
        private string Encode_EAN13()
        {
            //check length of input
            if (Raw_Data.Length < 12 || Raw_Data.Length > 13)
            {
                Error("EEAN13-1: Data length invalid. (Length must be 12 or 13)");
            }

            if (!BarcodeLib.Barcode.CheckNumericOnly(Raw_Data))
            {
                Error("EEAN13-2: Numeric Data Only");
            }

            string patterncode = EAN_Pattern[Int32.Parse(Raw_Data[0].ToString())];
            string result      = "101";

            //first
            //result += EAN_CodeA[Int32.Parse(RawData[0].ToString())];

            //second
            int pos = 0;

            while (pos < 6)
            {
                if (patterncode[pos] == 'a')
                {
                    result += EAN_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())];
                }
                if (patterncode[pos] == 'b')
                {
                    result += EAN_CodeB[Int32.Parse(Raw_Data[pos + 1].ToString())];
                }
                pos++;
            }//while


            //add divider bars
            result += "01010";

            //get the third
            pos = 1;
            while (pos <= 5)
            {
                result += EAN_CodeC[Int32.Parse(Raw_Data[(pos++) + 6].ToString())];
            }//while

            //checksum digit
            int cs = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString());

            //add checksum
            result += EAN_CodeC[cs];

            //add ending bars
            result += "101";

            //get the manufacturer assigning country
            init_CountryCodes();
            _Country_Assigning_Manufacturer_Code = "N/A";
            string twodigitCode   = Raw_Data.Substring(0, 2);
            string threedigitCode = Raw_Data.Substring(0, 3);

            try
            {
                _Country_Assigning_Manufacturer_Code = CountryCodes[threedigitCode].ToString();
            }//try
            catch
            {
                try
                {
                    _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString();
                }//try
                catch
                {
                    Error("EEAN13-3: Country assigning manufacturer code not found.");
                } //catch
            }     //catch
            finally { CountryCodes.Clear(); }

            return(result);
        }//Encode_EAN13
Example #15
0
        /// <summary>
        ///     Encode the raw data using the UPC-A algorithm.
        /// </summary>
        private string Encode_UPCA()
        {
            //check length of input
            if (Raw_Data.Length != 12)
            {
                throw new Exception("EUPCA-1: Data length invalid. (Length must be 12)");
            }

            if (!Barcode.CheckNumericOnly(Raw_Data))
            {
                throw new Exception("EUPCA-2: Numeric Data Only");
            }

            var result      = "101"; //start with guard bars
            var patterncode = UPC_Pattern[int.Parse(Raw_Data[0].ToString())];

            //first number
            result += UPC_CodeA[int.Parse(Raw_Data[0].ToString())];

            //second (group) of numbers
            var pos = 0;

            while (pos < 5)
            {
                if (patterncode[pos + 1] == 'a')
                {
                    result += UPC_CodeA[int.Parse(Raw_Data[pos + 1].ToString())];
                }
                if (patterncode[pos + 1] == 'b')
                {
                    result += UPC_CodeB[int.Parse(Raw_Data[pos + 1].ToString())];
                }
                pos++;
            }

            //add divider bars
            result += "01010";

            //third (group) of numbers
            pos = 0;
            while (pos < 5)
            {
                result += UPC_CodeC[int.Parse(Raw_Data[pos++ + 6].ToString())];
            }

            //forth
            result += UPC_CodeC[int.Parse(Raw_Data[Raw_Data.Length - 1].ToString())];

            //add ending guard bars
            result += "101";

            //get the manufacturer assigning country
            init_CountryCodes();
            var twodigitCode = "0" + Raw_Data.Substring(0, 1);

            try
            {
                _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString();
            }
            catch
            {
                throw new Exception("EUPCA-3: Country assigning manufacturer code not found.");
            }
            finally
            {
                CountryCodes.Clear();
            }

            return(result);
        }
Example #16
0
        /// <summary>
        /// Encode the raw data using the UPC Supplemental 5-digit algorithm.
        /// </summary>
        private string Encode_UPCSupplemental_5()
        {
            if (Raw_Data.Length != 5)
            {
                Error("EUPC-SUP5-1: Invalid data length. (Length = 5 required).");
            }

            if (!BarcodeLib.Barcode.CheckNumericOnly(Raw_Data))
            {
                Error("EUPCA-2: Numeric Data Only");
            }

            //calculate the checksum digit
            int even = 0;
            int odd  = 0;

            //odd
            for (int i = 0; i <= 4; i += 2)
            {
                odd += Int32.Parse(Raw_Data.Substring(i, 1)) * 3;
            }//for

            //even
            for (int i = 1; i < 4; i += 2)
            {
                even += Int32.Parse(Raw_Data.Substring(i, 1)) * 9;
            }//for

            int total = even + odd;
            int cs    = total % 10;

            string pattern = UPC_SUPP_5[cs];

            string result = "";

            int pos = 0;

            foreach (char c in pattern)
            {
                //Inter-character separator
                if (pos == 0)
                {
                    result += "1011";
                }
                else
                {
                    result += "01";
                }

                if (c == 'a')
                {
                    //encode using odd parity
                    result += EAN_CodeA[Int32.Parse(Raw_Data[pos].ToString())];
                }//if
                else if (c == 'b')
                {
                    //encode using even parity
                    result += EAN_CodeB[Int32.Parse(Raw_Data[pos].ToString())];
                } //else if
                pos++;
            }     //foreach
            return(result);
        }         //Encode_UPCSupplemental_5
Example #17
0
        /// <summary>
        /// Encode the raw data using the UPC Supplemental 5-digit algorithm.
        /// </summary>
        private string Encode_UPCSupplemental_5()
        {
            if (Raw_Data.Length != 5)
            {
                Error("EUPC-SUP5-1: Invalid data length. (Length = 5 required)");
            }

            if (!CheckNumericOnly(Raw_Data))
            {
                Error("EUPCA-2: Numeric Data Only");
            }

            //calculate the checksum digit
            var even = 0;
            var odd  = 0;

            //odd
            for (var i = 0; i <= 4; i += 2)
            {
                odd += Int32.Parse(Raw_Data.Substring(i, 1)) * 3;
            }//for

            //even
            for (var i = 1; i < 4; i += 2)
            {
                even += Int32.Parse(Raw_Data.Substring(i, 1)) * 9;
            }//for

            var total = even + odd;
            var cs    = total % 10;

            var pattern = UPC_SUPP_5[cs];

            var result = string.Empty;

            var pos = 0;

            foreach (var c in pattern)
            {
                //Inter-character separator
                if (pos == 0)
                {
                    result += "1011";
                }
                else
                {
                    result += "01";
                }

                switch (c)
                {
                case 'a':
                    //encode using odd parity
                    result += EAN_CodeA[Int32.Parse(Raw_Data[pos].ToString())];     //if
                    break;

                case 'b':
                    //encode using even parity
                    result += EAN_CodeB[Int32.Parse(Raw_Data[pos].ToString())];     //else if
                    break;
                }
                pos++;
            } //foreach
            return(result);
        }     //Encode_UPCSupplemental_5
Example #18
0
        /// <summary>
        ///     Encode the raw data using the EAN-13 algorithm. (Can include the checksum already.  If it doesnt exist in the data
        ///     then it will calculate it for you.  Accepted data lengths are 12 + 1 checksum or just the 12 data digits)
        /// </summary>
        private string Encode_EAN13()
        {
            //check length of input
            if (Raw_Data.Length < 12 || Raw_Data.Length > 13)
            {
                throw new Exception("EEAN13-1: Data length invalid. (Length must be 12 or 13)");
            }

            if (!Barcode.CheckNumericOnly(Raw_Data))
            {
                throw new Exception("EEAN13-2: Numeric Data Only");
            }

            var patterncode = EAN_Pattern[int.Parse(Raw_Data[0].ToString())];
            var result      = "101";

            var pos = 0;

            while (pos < 6)
            {
                if (patterncode[pos] == 'a')
                {
                    result += EAN_CodeA[int.Parse(Raw_Data[pos + 1].ToString())];
                }
                if (patterncode[pos] == 'b')
                {
                    result += EAN_CodeB[int.Parse(Raw_Data[pos + 1].ToString())];
                }
                pos++;
            }

            //add divider bars
            result += "01010";

            //get the third
            pos = 1;
            while (pos <= 5)
            {
                result += EAN_CodeC[int.Parse(Raw_Data[pos++ + 6].ToString())];
            }

            //checksum digit
            var cs = int.Parse(Raw_Data[Raw_Data.Length - 1].ToString());

            //add checksum
            result += EAN_CodeC[cs];

            //add ending bars
            result += "101";

            //get the manufacturer assigning country
            init_CountryCodes();
            _Country_Assigning_Manufacturer_Code = "N/A";
            var twodigitCode   = Raw_Data.Substring(0, 2);
            var threedigitCode = Raw_Data.Substring(0, 3);

            try
            {
                _Country_Assigning_Manufacturer_Code = CountryCodes[threedigitCode].ToString();
            }
            catch
            {
                try
                {
                    _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString();
                }
                catch
                {
                    throw new Exception("EEAN13-3: Country assigning manufacturer code not found.");
                }
            }
            finally
            {
                CountryCodes.Clear();
            }

            return(result);
        }
Example #19
0
        }//UPCE
        /// <summary>
        /// Encode the raw data using the UPC-E algorithm.
        /// </summary>
        private string Encode_UPCE()
        {
            if (Raw_Data.Length != 6 && Raw_Data.Length != 8 && Raw_Data.Length != 12) Error("EUPCE-1: Invalid data length. (8 or 12 numbers only)");

            if (!BarcodeLib.Barcode.CheckNumericOnly(Raw_Data)) Error("EUPCE-2: Numeric only.");

            int CheckDigit = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString());
            int NumberSystem = Int32.Parse(Raw_Data[0].ToString());

            //Convert to UPC-E from UPC-A if necessary
            if (Raw_Data.Length == 12)
            {
                string UPCECode = "";

                //break apart into components
                string Manufacturer = Raw_Data.Substring(1, 5);
                string ProductCode = Raw_Data.Substring(6, 5);

                //check for a valid number system
                if (NumberSystem != 0 && NumberSystem != 1)
                    Error("EUPCE-3: Invalid Number System (only 0 & 1 are valid)");

                if (Manufacturer.EndsWith("000") || Manufacturer.EndsWith("100") || Manufacturer.EndsWith("200") && Int32.Parse(ProductCode) <= 999)
                {
                    //rule 1
                    UPCECode += Manufacturer.Substring(0, 2); //first two of manufacturer
                    UPCECode += ProductCode.Substring(2, 3); //last three of product
                    UPCECode += Manufacturer[2].ToString(); //third of manufacturer
                }//if
                else if (Manufacturer.EndsWith("00") && Int32.Parse(ProductCode) <= 99)
                {
                    //rule 2
                    UPCECode += Manufacturer.Substring(0, 3); //first three of manufacturer
                    UPCECode += ProductCode.Substring(3, 2); //last two of product
                    UPCECode += "3"; //number 3
                }//else if
                else if (Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9)
                {
                    //rule 3
                    UPCECode += Manufacturer.Substring(0, 4); //first four of manufacturer
                    UPCECode += ProductCode[4]; //last digit of product
                    UPCECode += "4"; //number 4
                }//else if
                else if (!Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9 && Int32.Parse(ProductCode) >= 5)
                {
                    //rule 4
                    UPCECode += Manufacturer; //manufacturer
                    UPCECode += ProductCode[4]; //last digit of product
                }//else if
                else
                    Error("EUPCE-4: Illegal UPC-A entered for conversion.  Unable to convert.");

                Raw_Data = UPCECode;
            }//if

            //get encoding pattern 
            string pattern = "";

            if (NumberSystem == 0) pattern = UPCE_Code_0[CheckDigit];
            else pattern = UPCE_Code_1[CheckDigit];

            //encode the data
            string result = "101";

            int pos = 0;
            foreach (char c in pattern)
            {
                int i = Int32.Parse(Raw_Data[pos++].ToString());
                if (c == 'a')
                {
                    result += EAN_CodeA[i];
                }//if
                else if (c == 'b')
                {
                    result += EAN_CodeB[i];
                }//else if
            }//foreach

            //guard bars
            result += "01010";

            //end bars
            result += "1";

            return result;
        }//Encode_UPCE
Example #20
0
        /// <summary>
        ///     Encode the raw data using the UPC-E algorithm.
        /// </summary>
        private string Encode_UPCE()
        {
            if (Raw_Data.Length != 6 && Raw_Data.Length != 8 && Raw_Data.Length != 12)
            {
                throw new Exception("EUPCE-1: Invalid data length. (8 or 12 numbers only)");
            }

            if (!Barcode.CheckNumericOnly(Raw_Data))
            {
                throw new Exception("EUPCE-2: Numeric only.");
            }

            var CheckDigit   = int.Parse(Raw_Data[Raw_Data.Length - 1].ToString());
            var NumberSystem = int.Parse(Raw_Data[0].ToString());

            //Convert to UPC-E from UPC-A if necessary
            if (Raw_Data.Length == 12)
            {
                var UPCECode = string.Empty;

                //break apart into components
                var Manufacturer = Raw_Data.Substring(1, 5);
                var ProductCode  = Raw_Data.Substring(6, 5);

                //check for a valid number system
                if (NumberSystem != 0 && NumberSystem != 1)
                {
                    throw new Exception("EUPCE-3: Invalid Number System (only 0 & 1 are valid)");
                }

                if (Manufacturer.EndsWith("000") || Manufacturer.EndsWith("100") ||
                    Manufacturer.EndsWith("200") && int.Parse(ProductCode) <= 999)
                {
                    //rule 1
                    UPCECode += Manufacturer.Substring(0, 2); //first two of manufacturer
                    UPCECode += ProductCode.Substring(2, 3);  //last three of product
                    UPCECode += Manufacturer[2].ToString();   //third of manufacturer
                }
                else if (Manufacturer.EndsWith("00") && int.Parse(ProductCode) <= 99)
                {
                    //rule 2
                    UPCECode += Manufacturer.Substring(0, 3); //first three of manufacturer
                    UPCECode += ProductCode.Substring(3, 2);  //last two of product
                    UPCECode += "3";                          //number 3
                }
                else if (Manufacturer.EndsWith("0") && int.Parse(ProductCode) <= 9)
                {
                    //rule 3
                    UPCECode += Manufacturer.Substring(0, 4); //first four of manufacturer
                    UPCECode += ProductCode[4];               //last digit of product
                    UPCECode += "4";                          //number 4
                }
                else if (!Manufacturer.EndsWith("0") && int.Parse(ProductCode) <= 9 && int.Parse(ProductCode) >= 5)
                {
                    //rule 4
                    UPCECode += Manufacturer;   //manufacturer
                    UPCECode += ProductCode[4]; //last digit of product
                }
                else
                {
                    throw new Exception("EUPCE-4: Illegal UPC-A entered for conversion.  Unable to convert.");
                }

                Raw_Data = UPCECode;
            }

            //get encoding pattern
            var pattern = string.Empty;

            if (NumberSystem == 0)
            {
                pattern = UPCE_Code_0[CheckDigit];
            }
            else
            {
                pattern = UPCE_Code_1[CheckDigit];
            }

            //encode the data
            var result = "101";

            var pos = 0;

            foreach (var c in pattern)
            {
                var i = int.Parse(Raw_Data[pos++].ToString());
                if (c == 'a')
                {
                    result += EAN_CodeA[i];
                } //if
                else if (c == 'b')
                {
                    result += EAN_CodeB[i];
                }
            }

            //guard bars
            result += "01010";

            //end bars
            result += "1";

            return(result);
        }
Example #21
0
        /// <summary>
        ///     Encode the raw data using the UPC Supplemental 5-digit algorithm.
        /// </summary>
        private string Encode_UPCSupplemental_5()
        {
            if (Raw_Data.Length != 5)
            {
                throw new Exception("EUPC-SUP5-1: Invalid data length. (Length = 5 required).");
            }

            if (!Barcode.CheckNumericOnly(Raw_Data))
            {
                throw new Exception("EUPCA-2: Numeric Data Only");
            }

            //calculate the checksum digit
            var even = 0;
            var odd  = 0;

            //odd
            for (var i = 0; i <= 4; i += 2)
            {
                odd += int.Parse(Raw_Data.Substring(i, 1)) * 3;
            }

            //even
            for (var i = 1; i < 4; i += 2)
            {
                even += int.Parse(Raw_Data.Substring(i, 1)) * 9;
            }

            var total = even + odd;
            var cs    = total % 10;

            var pattern = UPC_SUPP_5[cs];

            var result = string.Empty;

            var pos = 0;

            foreach (var c in pattern)
            {
                //Inter-character separator
                if (pos == 0)
                {
                    result += "1011";
                }
                else
                {
                    result += "01";
                }

                if (c == 'a')
                {
                    //encode using odd parity
                    result += EAN_CodeA[int.Parse(Raw_Data[pos].ToString())];
                }
                else if (c == 'b')
                {
                    //encode using even parity
                    result += EAN_CodeB[int.Parse(Raw_Data[pos].ToString())];
                }
                pos++;
            }
            return(result);
        }
Example #22
0
        private string Encode_EAN13()
        {
            if (Raw_Data.Length < 12 || Raw_Data.Length > 13)
            {
                Error("EEAN13-1: Data length invalid. (Length must be 12 or 13)");
            }

            if (!CheckNumericOnly(Raw_Data))
            {
                Error("EEAN13-2: Numeric Data Only");
            }

            string patterncode = EAN_Pattern[Int32.Parse(Raw_Data[0].ToString())];
            string result      = "101";

            int pos = 0;

            while (pos < 6)
            {
                if (patterncode[pos] == 'a')
                {
                    result += EAN_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())];
                }
                if (patterncode[pos] == 'b')
                {
                    result += EAN_CodeB[Int32.Parse(Raw_Data[pos + 1].ToString())];
                }
                pos++;
            }


            result += "01010";

            pos = 1;
            while (pos <= 5)
            {
                result += EAN_CodeC[Int32.Parse(Raw_Data[(pos++) + 6].ToString())];
            }

            int cs = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString());

            result += EAN_CodeC[cs];

            result += "101";

            Init_CountryCodes();
            _Country_Assigning_Manufacturer_Code = "N/A";
            string twodigitCode   = Raw_Data.Substring(0, 2);
            string threedigitCode = Raw_Data.Substring(0, 3);

            try
            {
                _Country_Assigning_Manufacturer_Code = CountryCodes[threedigitCode].ToString();
            }
            catch
            {
                try
                {
                    _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString();
                }
                catch
                {
                    Error("EEAN13-3: Country assigning manufacturer code not found.");
                }
            }
            finally { CountryCodes.Clear(); }

            return(result);
        }
Example #23
0
        /// <summary>
        /// Encode the raw data using the EAN-13 algorithm. (Can include the checksum already.  If it doesnt exist in the data then it will calculate it for you.  Accepted data lengths are 12 + 1 checksum or just the 12 data digits)
        /// </summary>
        private string Encode_EAN13()
        {
            //check length of input
            if (Raw_Data.Length < 12 || Raw_Data.Length > 13)
            {
                Error("EEAN13-1: Data length invalid. (Length must be 12 or 13)");
            }

            if (!CheckNumericOnly(Raw_Data))
            {
                Error("EEAN13-2: Numeric Data Only");
            }

            var patterncode = EAN_Pattern[Int32.Parse(Raw_Data[0].ToString())];
            var result      = "101";

            //first
            //result += EAN_CodeA[Int32.Parse(RawData[0].ToString())];

            //second
            var pos = 0;

            while (pos < 6)
            {
                if (patterncode[pos] == 'a')
                {
                    result += EAN_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())];
                }
                if (patterncode[pos] == 'b')
                {
                    result += EAN_CodeB[Int32.Parse(Raw_Data[pos + 1].ToString())];
                }
                pos++;
            }//while


            //add divider bars
            result += "01010";

            //get the third
            pos = 1;
            while (pos <= 5)
            {
                result += EAN_CodeC[Int32.Parse(Raw_Data[(pos++) + 6].ToString())];
            }//while

            //checksum digit
            var cs = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString());

            //add checksum
            result += EAN_CodeC[cs];

            //add ending bars
            result += "101";

            //get the manufacturer assigning country
            Init_CountryCodes();
            _countryAssigningManufacturerCode = "N/A";
            var twodigitCode   = Raw_Data.Substring(0, 2);
            var threedigitCode = Raw_Data.Substring(0, 3);

            var cc = _countryCodes[threedigitCode];

            if (cc == null)
            {
                cc = _countryCodes[twodigitCode].ToString();
                if (cc == null)
                {
                    Error("EEAN13-3: Country assigning manufacturer code not found.");
                }
                else
                {
                    _countryAssigningManufacturerCode = cc.ToString();
                }
            }
            else
            {
                _countryAssigningManufacturerCode = cc.ToString();
            }

            _countryCodes.Clear();

            return(result);
        }//Encode_EAN13
Example #24
0
        private string Encode_UPCE()
        {
            if (Raw_Data.Length != 6 && Raw_Data.Length != 8 && Raw_Data.Length != 12)
            {
                Error("EUPCE-1: Invalid data length. (8 or 12 numbers only)");
            }

            if (!CheckNumericOnly(Raw_Data))
            {
                Error("EUPCE-2: Numeric only.");
            }

            int NumberSystem = Int32.Parse(Raw_Data[0].ToString());

            if (NumberSystem != 0 && NumberSystem != 1)
            {
                Error("EUPCE-3: Invalid Number System (only 0 & 1 are valid)");
            }

            int CheckDigit = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString());

            if (Raw_Data.Length == 12)
            {
                string UPCECode = "";

                string Manufacturer = Raw_Data.Substring(1, 5);
                string ProductCode  = Raw_Data.Substring(6, 5);

                if (Manufacturer.EndsWith("000") || Manufacturer.EndsWith("100") || Manufacturer.EndsWith("200") && Int32.Parse(ProductCode) <= 999)
                {
                    UPCECode += Manufacturer.Substring(0, 2);
                    UPCECode += ProductCode.Substring(2, 3);
                    UPCECode += Manufacturer[2].ToString();
                }
                else if (Manufacturer.EndsWith("00") && Int32.Parse(ProductCode) <= 99)
                {
                    UPCECode += Manufacturer.Substring(0, 3);
                    UPCECode += ProductCode.Substring(3, 2);
                    UPCECode += "3";
                }
                else if (Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9)
                {
                    UPCECode += Manufacturer.Substring(0, 4);
                    UPCECode += ProductCode[4];
                    UPCECode += "4";
                }
                else if (!Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9 && Int32.Parse(ProductCode) >= 5)
                {
                    UPCECode += Manufacturer;
                    UPCECode += ProductCode[4];
                }
                else
                {
                    Error("EUPCE-4: Illegal UPC-A entered for conversion.  Unable to convert.");
                }

                Raw_Data = UPCECode;
            }

            string pattern = "";

            if (NumberSystem == 0)
            {
                pattern = UPCE_Code_0[CheckDigit];
            }
            else
            {
                pattern = UPCE_Code_1[CheckDigit];
            }

            string result = "101";

            int pos = 0;

            foreach (char c in pattern)
            {
                int i = Int32.Parse(Raw_Data[++pos].ToString());
                if (c == 'a')
                {
                    result += EAN_CodeA[i];
                }
                else if (c == 'b')
                {
                    result += EAN_CodeB[i];
                }
            }

            result += "01010";

            result += "1";

            return(result);
        }