Skip to content
jdubs edited this page Oct 21, 2016 · 1 revision

Table of Contents

Overview

Calculates the European Article Number (EAN) barcoding standard to the supplied variable parameter.

Parameters

SpecName (constant) The variable containing the input to be converted. Proper EAN13 font must be applied in the template.

ErrorMessage (multiple | optional) The error message to return when any error occurs.

Synopsis

This method follows the specifications of the European Article Number (EAN), which is a subset of the original 12-digit UPC system developed in North America. The EAN-13 barcode is defined by the standards organization GS1.

Source Code

public static void EAN13(string SpecName, object ErrorMessage)
{
	Break();
	try
	{
		string barcode;
		string submission = Variable(SpecName).Value;
		double checkDigitSubtotal;
		string checkDigit;

		char[] chars = submission.ToCharArray();

		if (chars.Length != 12)
			throw new Four51ActionsException("EAN13 barcode must be 12 digits");

		checkDigitSubtotal = 3 * (Char.GetNumericValue(chars[1]) + Char.GetNumericValue(chars[3]) + Char.GetNumericValue(chars[5]) + Char.GetNumericValue(chars[7]) + Char.GetNumericValue(chars[9]) + Char.GetNumericValue(chars[11]));
		checkDigitSubtotal += Char.GetNumericValue(chars[0]) + Char.GetNumericValue(chars[2]) + Char.GetNumericValue(chars[4]) + Char.GetNumericValue(chars[6]) + Char.GetNumericValue(chars[8]) + Char.GetNumericValue(chars[10]);
		checkDigit = (300 - checkDigitSubtotal).ToString();
		checkDigit = checkDigit.Substring(checkDigit.Length - 1, 1);

		barcode = (char)(194 + Char.GetNumericValue(chars[0])) + "x" + OddBar(chars[1].ToString());

		switch (chars[0].ToString())
		{
			case "0":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, OddBar(chars[2].ToString()), OddBar(chars[3].ToString()), OddBar(chars[4].ToString()), OddBar(chars[5].ToString()), OddBar(chars[6].ToString()));
				break;
			case "1":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, OddBar(chars[2].ToString()), EvenBar(chars[3].ToString()), OddBar(chars[4].ToString()), EvenBar(chars[5].ToString()), EvenBar(chars[6].ToString()));
				break;
			case "2":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, OddBar(chars[2].ToString()), EvenBar(chars[3].ToString()), EvenBar(chars[4].ToString()), OddBar(chars[5].ToString()), EvenBar(chars[6].ToString()));
				break;
			case "3":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, OddBar(chars[2].ToString()), EvenBar(chars[3].ToString()), EvenBar(chars[4].ToString()), EvenBar(chars[5].ToString()), OddBar(chars[6].ToString()));
				break;
			case "4":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, EvenBar(chars[2].ToString()), OddBar(chars[3].ToString()), OddBar(chars[4].ToString()), EvenBar(chars[5].ToString()), EvenBar(chars[6].ToString()));
				break;
			case "5":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, EvenBar(chars[2].ToString()), EvenBar(chars[3].ToString()), OddBar(chars[4].ToString()), OddBar(chars[5].ToString()), EvenBar(chars[6].ToString()));
				break;
			case "6":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, EvenBar(chars[2].ToString()), EvenBar(chars[3].ToString()), EvenBar(chars[4].ToString()), OddBar(chars[5].ToString()), OddBar(chars[6].ToString()));
				break;
			case "7":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, EvenBar(chars[2].ToString()), OddBar(chars[3].ToString()), EvenBar(chars[4].ToString()), OddBar(chars[5].ToString()), EvenBar(chars[6].ToString()));
				break;
			case "8":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, EvenBar(chars[2].ToString()), OddBar(chars[3].ToString()), EvenBar(chars[4].ToString()), EvenBar(chars[5].ToString()), OddBar(chars[6].ToString()));
				break;
			case "9":
				barcode = String.Format("{0}{1}{2}{3}{4}{5}", barcode, EvenBar(chars[2].ToString()), EvenBar(chars[3].ToString()), OddBar(chars[4].ToString()), EvenBar(chars[5].ToString()), OddBar(chars[6].ToString()));
				break;
		}

		Variable(SpecName).Value = String.Format("{0}y{1}{2}z", barcode, submission.Substring(7, 5), checkDigit);
	}
	catch (Exception ex)
	{
		throw new Four51ActionsException(ex.Message, (string)ErrorMessage);
	}
}

Referenced Methods

Clone this wiki locally