The standard 2 of 5 barcode format encodes all of the information in the bars, and only uses the spaces for spacing purposes, unlike all the other barcodes we have looked at so far, and is used mostly in warehouses and airline ticketing.


To calculate the checksum digit, working through the digits from right to left, you add up the odd-positioned elements and even-positioned elements separately. We then find the checksum by 10 – ((((3 * odd) + even) modulo 10) modulo 10).

Using this full string then we apply the encoding, where a narrow bar is defined by 11 and a wide bar with 111111. The left guard is 1111011110110 and the right guard is 1111011011110.

The encodings for each digit is as follows

0 11011011111101111110110
1 11111101101101101111110
2 11011111101101101111110
3 11111101111110110110110
4 11011011111101101111110
5 11111101101111110110110
6 11011111101111110110110
7 11011011011111101111110
8 11111101101101111110110
9 11011111101101111110110


Here is the code to encode the barcode. The full source code is available here

namespace BarcoderLib
{
    public class BarcodeStandard2of5
    {
        private string gLeftGuard = "1111011110110";
        private string gRightGuard = "1111011011110";
        private string[] gCoding = { "11011011111101111110110", "11111101101101101111110", "11011111101101101111110", 
                                     "11111101111110110110110", "11011011111101101111110", "11111101101111110110110",
                                     "11011111101111110110110", "11011011011111101111110", "11111101101101111110110", "11011111101101111110110" };
        public Bitmap Encode(string message)
        {
            string encodedMessage;
            string fullMessage;

            Bitmap barcodeImage = new Bitmap(400, 100);
            Graphics g = Graphics.FromImage(barcodeImage);

            Validate(message);

            fullMessage = message + CalcParity(message).ToString().Trim();
            encodedMessage = EncodeBarcode(fullMessage);

            PrintBarcode(g, encodedMessage, fullMessage, 350, 100);

            return barcodeImage;
        }
        private void Validate(string message)
        {

            Regex reNum = new Regex(@"^\d+$");
            if (reNum.Match(message).Success == false)
            {
                throw new Exception("Encode string must be numeric");
            }

        }

        private void PrintBarcode(Graphics g, string encodedMessage, string message, int width, int height)
        {
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            Font textFont = new Font(FontFamily.GenericMonospace, 12, FontStyle.Regular);
            g.FillRectangle(whiteBrush, 0, 0, width, height);

            int xPos = 20;
            int yTop = 10;
            int barHeight = 50;

            for (int i = 0; i < encodedMessage.Length; i++)
            {
                if (encodedMessage[i] == '1')
                {
                    g.FillRectangle(blackBrush, xPos, yTop, 1, barHeight);
                }
                xPos += 1;
            }

            xPos = 20;
            yTop += barHeight - 2;
            for (int i = 0; i < message.Length; i++)
            {
                g.DrawString(message[i].ToString().Trim(), textFont, blackBrush, xPos, yTop);
                xPos += 7;
            }
        }

        private string EncodeBarcode(string message)
        {
            int i;
            string encodedString = gLeftGuard;

            for (i = 0; i < message.Length; i++)
            {
                encodedString += gCoding[Convert.ToInt32(message[i].ToString())];
            }

            encodedString += gRightGuard;

            return encodedString;
        }

        private int CalcParity(string message)
        {
            int oddSum = 0;
            int evenSum = 0;
            int parity = 0;

            for (int i = message.Length - 1; i >= 0; i--)
            {
                if ((i % 2) == 0)
                {
                    evenSum += Convert.ToInt32(message[i].ToString());
                }else
                {
                    oddSum += Convert.ToInt32(message[i].ToString());
                }
            }

            parity = 10 - ((((3 * oddSum) + evenSum) % 10) % 10);
            if (parity == 10)
            {
                parity = 0;
            }
            return parity;
        }
    }
}

Share