UPCVersionA
jdubs edited this page Oct 21, 2016
·
1 revision
Enforces the symbology of the Universal Product Code (UPC) and returns the calculated pattern.
SpecName (constant) The variable containing the input to be converted. Proper UPC font must be applied in the template.
ErrorMessage (multiple | optional) The error message to return when any error occurs.
This method follows the specifications of the Universal Product Code (UPC) system developed in North America. The UPC barcode is defined by the standards organization GS1.
public static void UPCVersionA(string SpecName, object ErrorMessage)
{
Break();
try
{
double sum;
double x = 0; // the odd numbered digits added then multiplied by 3
double y = 0; // the even numbered digits added then subtracted from the next-highest multiple of ten
string barcode = String.Empty;
char[] chars = Variable(SpecName).Value.ToCharArray();
if (chars.Length != 11)
throw new Four51ActionsException("UPC-A barcode must be 11 digits");
// string substitution for the 1st character
barcode = CharReplacement(chars[0], BarcodeSymbology.UPCA).ToString();
// string substitution for the left notch
for (int i = 1; i < 6; i++)
{
barcode += ((char)(Char.GetNumericValue(chars[i]) + 65)).ToString();
}
// stop start char for middle of barcode
barcode += "y";
// the third set does not get encoded
for (int i = 6; i <= chars.GetUpperBound(0); i++)
{
barcode += chars[i].ToString();
}
// calculate checksum
for (int i = 0; i <= chars.GetUpperBound(0); i++)
{
if ((i % 2) != 0)
y += Convert.ToInt32(chars[i].ToString());
else
x += Convert.ToInt32(chars[i].ToString());
}
sum = (x * 3) + y;
sum = (10 - (sum % 10)) % 10;
barcode += String.Format("{0}z{1}",
((char)(sum + 107)).ToString(),
CharReplacement(sum.ToString().ToCharArray()[0], BarcodeSymbology.UPCA_checkdigit).ToString());
Variable(SpecName).Value = barcode.ToString();
}
catch (Exception ex)
{
throw new Four51ActionsException(ex.Message, (string)ErrorMessage);
}
}