Ejemplo n.º 1
0
        /// <summary>
        ///     Encode the raw data using the EAN-8 algorithm.
        /// </summary>
        private string Encode_EAN8()
        {
            //check length
            if (Raw_Data.Length != 8 && Raw_Data.Length != 7)
            {
                throw new Exception("EEAN8-1: Invalid data length. (7 or 8 numbers only)");
            }

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

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

            //first half (Encoded using left hand / odd parity)
            for (var i = 0; i < Raw_Data.Length / 2; i++)
            {
                result += EAN_CodeA[int.Parse(Raw_Data[i].ToString())];
            }

            //center guard bars
            result += "01010";

            //second half (Encoded using right hand / even parity)
            for (var i = Raw_Data.Length / 2; i < Raw_Data.Length; i++)
            {
                result += EAN_CodeC[int.Parse(Raw_Data[i].ToString())];
            }

            result += "101";

            return(result);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Encode the raw data using the Code 11 algorithm.
        /// </summary>
        private string Encode_Code11()
        {
            if (!Barcode.CheckNumericOnly(Raw_Data.Replace("-", string.Empty)))
            {
                throw new Exception("EC11-1: Numeric data and '-' Only");
            }

            //calculate the checksums
            var weight = 1;
            var CTotal = 0;
            var Data_To_Encode_with_Checksums = Raw_Data;

            //figure the C checksum
            for (var i = Raw_Data.Length - 1; i >= 0; i--)
            {
                //C checksum weights go 1-10
                if (weight == 10)
                {
                    weight = 1;
                }

                if (Raw_Data[i] != '-')
                {
                    CTotal += int.Parse(Raw_Data[i].ToString()) * weight++;
                }
                else
                {
                    CTotal += 10 * weight++;
                }
            }
            var checksumC = CTotal % 11;

            Data_To_Encode_with_Checksums += checksumC.ToString();

            //K checksums are recommended on any message length greater than or equal to 10
            if (Raw_Data.Length >= 1)
            {
                weight = 1;
                var KTotal = 0;

                //calculate K checksum
                for (var i = Data_To_Encode_with_Checksums.Length - 1; i >= 0; i--)
                {
                    //K checksum weights go 1-9
                    if (weight == 9)
                    {
                        weight = 1;
                    }

                    if (Data_To_Encode_with_Checksums[i] != '-')
                    {
                        KTotal += int.Parse(Data_To_Encode_with_Checksums[i].ToString()) * weight++;
                    }
                    else
                    {
                        KTotal += 10 * weight++;
                    }
                }
                var checksumK = KTotal % 11;
                Data_To_Encode_with_Checksums += checksumK.ToString();
            }

            //encode data
            var space  = "0";
            var result = C11_Code[11] + space; //start-stop char + interchar space

            foreach (var c in Data_To_Encode_with_Checksums)
            {
                var index = c == '-' ? 10 : int.Parse(c.ToString());
                result += C11_Code[index];

                //inter-character space
                result += space;
            }

            //stop bars
            result += C11_Code[11];

            return(result);
        }
Ejemplo n.º 5
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);
        }
        /// <summary>
        ///     Encode the raw data using the Interleaved 2 of 5 algorithm.
        /// </summary>
        private string Encode_Interleaved2of5()
        {
            //check length of input
            if (Raw_Data.Length % 2 != 0)
            {
                throw new Exception("EI25-1: Data length invalid.");
            }

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

            var result = "1010";

            for (var i = 0; i < Raw_Data.Length; i += 2)
            {
                var bars          = true;
                var patternbars   = I25_Code[int.Parse(Raw_Data[i].ToString())];
                var patternspaces = I25_Code[int.Parse(Raw_Data[i + 1].ToString())];
                var patternmixed  = string.Empty;

                //interleave
                while (patternbars.Length > 0)
                {
                    patternmixed += patternbars[0] + patternspaces[0].ToString();
                    patternbars   = patternbars.Substring(1);
                    patternspaces = patternspaces.Substring(1);
                }

                foreach (var c1 in patternmixed)
                {
                    if (bars)
                    {
                        if (c1 == 'N')
                        {
                            result += "1";
                        }
                        else
                        {
                            result += "11";
                        }
                    }
                    else
                    {
                        if (c1 == 'N')
                        {
                            result += "0";
                        }
                        else
                        {
                            result += "00";
                        }
                    }
                    bars = !bars;
                }
            }

            //add ending bars
            result += "1101";
            return(result);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Encode the raw data using the MSI algorithm.
        /// </summary>
        private string Encode_MSI()
        {
            //check for non-numeric chars
            if (!Barcode.CheckNumericOnly(Raw_Data))
            {
                throw new Exception("EMSI-1: Numeric Data Only");
            }

            var PreEncoded = Raw_Data;

            //get checksum
            if (Encoded_Type == TYPE.MSI_Mod10 || Encoded_Type == TYPE.MSI_2Mod10)
            {
                var odds  = string.Empty;
                var evens = string.Empty;
                for (var i = PreEncoded.Length - 1; i >= 0; i -= 2)
                {
                    odds = PreEncoded[i] + odds;
                    if (i - 1 >= 0)
                    {
                        evens = PreEncoded[i - 1] + evens;
                    }
                }

                //multiply odds by 2
                odds = Convert.ToString(int.Parse(odds) * 2);

                var evensum = 0;
                var oddsum  = 0;
                foreach (var c in evens)
                {
                    evensum += int.Parse(c.ToString());
                }
                foreach (var c in odds)
                {
                    oddsum += int.Parse(c.ToString());
                }
                var checksum = 10 - (oddsum + evensum) % 10;
                PreEncoded += checksum.ToString();
            }

            if (Encoded_Type == TYPE.MSI_Mod11 || Encoded_Type == TYPE.MSI_Mod11_Mod10)
            {
                var sum    = 0;
                var weight = 2;
                for (var i = PreEncoded.Length - 1; i >= 0; i--)
                {
                    if (weight > 7)
                    {
                        weight = 2;
                    }
                    sum += int.Parse(PreEncoded[i].ToString()) * weight++;
                }
                var checksum = 11 - sum % 11;

                PreEncoded += checksum.ToString();
            }

            if (Encoded_Type == TYPE.MSI_2Mod10 || Encoded_Type == TYPE.MSI_Mod11_Mod10)
            {
                //get second check digit if 2 mod 10 was selected or Mod11/Mod10
                var odds  = string.Empty;
                var evens = string.Empty;
                for (var i = PreEncoded.Length - 1; i >= 0; i -= 2)
                {
                    odds = PreEncoded[i] + odds;
                    if (i - 1 >= 0)
                    {
                        evens = PreEncoded[i - 1] + evens;
                    }
                }

                //multiply odds by 2
                odds = Convert.ToString(int.Parse(odds) * 2);

                var evensum = 0;
                var oddsum  = 0;
                foreach (var c in evens)
                {
                    evensum += int.Parse(c.ToString());
                }
                foreach (var c in odds)
                {
                    oddsum += int.Parse(c.ToString());
                }
                var checksum = 10 - (oddsum + evensum) % 10;
                PreEncoded += checksum.ToString();
            }

            var result = "110";

            foreach (var c in PreEncoded)
            {
                result += MSI_Code[int.Parse(c.ToString())];
            }

            //add stop character
            result += "1001";

            return(result);
        }