I'm currently working on a windows form in VS2010 that is supposed to create UPC-A for new part numbers and store them in a database. So far I'm still trying to get the pieces together and I'm having issues with calculating the check digit. I came up with the following two scenarios:
Scenario 1: Do the calculation the way we currently do it in Excel:
string value = uPCDataSet.UPC.Rows[uPCDataSet.UPC.Rows.Count - 1]["UPCNumber"].ToString();
string manCode = value.Substring(1, 5);
string prodCode = value.Substring(7, 5);
int first = Math.Abs(Convert.ToInt32(value));
while (first >= 10)
first /= 10;
int chkDigitSubtotal;
string UPCNumber = first + manCode + prodCode;
chkDigitSubtotal = Convert.ToInt32((UPCNumber.Substring(1, 1)) + (UPCNumber.Substring(3, 1)) +
(UPCNumber.Substring(5, 1)) + (UPCNumber.Substring(7, 1)) + (UPCNumber.Substring(9, 1)) +
(UPCNumber.Substring(11, 1)));
chkDigitSubtotal = (3 * chkDigitSubtotal) + Convert.ToInt32((UPCNumber.Substring(2, 1)) +
(UPCNumber.Substring(4, 1)) + (UPCNumber.Substring(6, 1)) + (UPCNumber.Substring(8, 1)) +
(UPCNumber.Substring(10, 1)));
string chkDigit = ((300 - chkDigitSubtotal).ToString()).Substring(12, 1);
When I run this code, I get an error "Value was either too large or too small for an Int32". I don't understand why since "value" is a varchar
with 12 characters (they are all numbers).
Scenario 2: I found a code snipped online. This code works, but gives me the wrong check digit. I'm using a UPC that we calculated using your Excel sheet:
string value = "606891001678"; //valid UPC with check digit
string UPCBody = value.Substring(0, 11);
int sum = 0;
bool odd = true;
for (int i = value.Length - 1; i >= 0; i--)
{
if (odd == true)
{
int tSum = Convert.ToInt32(value[i].ToString()) * 2;
if (tSum >= 10)
{
string tData = tSum.ToString();
tSum = Convert.ToInt32(tData[0].ToString()) + Convert.ToInt32(tData[1].ToString());
}
sum += tSum;
}
else
sum += Convert.ToInt32(value[i].ToString());
odd = !odd;
}
int result = (((sum / 10)+1) *10) - sum;
result = result % 10;
MessageBox.Show(UPCBody + " & " + result);
In this example the check digit obviously should be 8, but it returns 2. I've spent pretty much all day on this and could use some advice. Why am I getting the error on the first scenario and why am I getting a different check digit in the second scenario from what it should be?
Thanks in advance for your help!
Chris
EDIT:
I deleted two previous edits since the code was flawed in a sense that I didn't get the correct results. I found out that we use Azalea barcodes and they have their own method of calculating the check digit (see ChkDigitSubtotal). So the below code works and gives me the correct check digit for my scenario. I think this code will work up to chkDigitSubtotal for all UPC-A calculations. Thanks to Harrison for his great guidance and resourceful answer!
string value = uPCDataSet.UPC.Rows[uPCDataSet.UPC.Rows.Count - 1]
["UPCNumber"].ToString();
long chkDigitOdd;
long chkDigitEven;
long chkDigitSubtotal;
string UPCNumber = value.Substring(0, 11);
chkDigitOdd = Convert.ToInt64(UPCNumber.Substring(0, 1)) +
Convert.ToInt64(UPCNumber.Substring(2, 1)) + Convert.ToInt64(UPCNumber.Substring(4, 1))
+ Convert.ToInt64(UPCNumber.Substring(6, 1)) + Convert.ToInt64(UPCNumber.Substring(8,
1)) + Convert.ToInt64(UPCNumber.Substring(10, 1));
chkDigitOdd = (3 * chkDigitOdd);
chkDigitEven = Convert.ToInt64(UPCNumber.Substring(1, 1)) +
Convert.ToInt64(UPCNumber.Substring(3, 1)) + Convert.ToInt64(UPCNumber.Substring(5, 1))
+ Convert.ToInt64(UPCNumber.Substring(7, 1)) + Convert.ToInt64(UPCNumber.Substring(9,
1));
chkDigitSubtotal = (300-(chkDigitEven + chkDigitOdd));
string chkDigit = chkDigitSubtotal.ToString();
chkDigit = chkDigit.Substring(chkDigit.Length - 1, 1);