5.3. UPC-A

UPC-A is widely used in the United States and Canada to identify retail products at the time of checkout. A UPC-A symbol has a fixed length of 12 digits, with the last digit served as a checksum. The first digit is called “number system”, followed by the manufacturer code and product code. The Uniform Code Council has the authority assigning manufacturer code. The remaining digits, the product code, is assigned by the manufacturer. This arrangement ensures that every retail proudct has its unqiue identification number.

The UPC-A encoding scheme is relativley complicated. A digit has two different encodings depending on the position of the digit. A complete barcode string contains 15 characters, divided into 7 parts: [3]

Character Index Pattern
1 Left Guard
2-7 6-digit left halve
8 Center Guard
9-14 6-digit right halve
15 Right Guard

The left guard and right guard have the same encoding patterns. They can be printed using asterisk, left and right parenthesis or brackets: *, ( and ), or [ and ]. The center guard can be printed using either a vertical bar symbol | or the hyphen -.

The patterns for the left halve digits are represented by 0-9 respectively; and the right halve digits are mapped to A-J:

Digit Left Halve Right Halve
0 0 A
1 1 B
2 2 C
3 3 D
4 4 E
5 5 F
6 6 G
7 7 H
8 8 I
9 9 J

For example, suppose we wish to encode the number 543000186706. The following three lines produce identical barcodes:

    *543000|BIGHAG*
    (543000-BIGHAG)
    [543000|BIGHAG] 

5.3.1. Calculate UPC-A Check Digit

A UPC-A data consists of 12 digits in total. The last digit is derived from the previous 11 digigts based on modulo 10 algorithm [4]. To calucalte the check digit, follow the steps below:

  1. From the right to left, start with odd position, assign the odd/even position to each digit.

  2. Sum all digits in odd position and multiply the result by 3.

  3. Sum all digits in even position.

  4. Sum the results of step 3 and step 4.

  5. Divide the result of step 4 by 10. The check digit is the number which adds the remainder to 10.

For example, assume that we want to calculate the check digit of UPC-A number 72641217542.

Procedure 5.1. Calculating check digit for UPC-A number 72641217542

  1. Step 1

    Arrange a two-row matrix, labelled from 1 to 11, with 1 being the leftmost position. Copy each digit into the matrix.

    From the right to the left, start with odd position, assign the odd/even position to each digit.

    Index 1 2 3 4 5 6 7 8 9 10 11
    Digit 7 2 6 4 1 2 1 7 5 4 2
    Position O E O E O E O E O E O
  2. Step 2

    Sum all digits in odd position, and multiply the result by 3.

    7 + 6 + 1 + 1 + 5 + 7 + 2 = 22 
    22 X 3 = 66 
  3. Step 3

    Sum all digits in even position.

    2 + 4 + 2 + 7 + 4 = 19 
  4. Step 4

    Sum the results of step 3 and 4.

    66 + 19 = 85  
  5. Step 5

    Divide the result of step 4 by 10. The check digit is the number which adds the remainder to 10. In this example, divide 85 by 10 results in remainder 5. Thus, 5 is the check digit since it results in 10 when added to 5.

    Append the check digit to the data we get the full UPC-A code: 726412175425.

Refer to the code in the following section for a sample implementation.

C Code

The following C code illustrates the check digit calculation and the barcode string generation.

static char barcode_string[20];
int upc_a_check_digit(char* data)
{
	int sum, check_digit;
	char* p;
	sum=0;
	sum = (data[0]+data[2]+data[4]+data[6]+data[8]+data[10])*3 +
          (data[1]+data[3]+data[5]+data[7]+data[9]+data[11]);
	check_digit = 10-(sum %10 );
	if (check_digit==10 ) check_digit=0;
	return check_digit;
}

char* upc_barcode_string(char* data)
{
	char* p;
	int i;
	int check_digit;

	memset(barcode_string, 0, sizeof(barcode_string));
	check_digit=upc_a_check_digit(data);
	barcode_string[0]='[';
	for(i=0; i<6; i++)
		barcode_string[i+1]=data[i];
    barcode_string[7]='|';
    for(i=6; i<10; i++)
	    barcode_string[i+2]=data[i]-'0'+'A';
    barcode_string[13]=check_digit +'A';
    barcode_string[14]=']';
    return barcode_string;
}

BASIC Code

Function upc_a_check_digit(data as String) As Integer
	Dim sum as Integer
	Dim i As Integer
	
	sum=0
	for i=1 to 11 Step 2
		sum = sum + Val(Mid(data,I,1))
	Next i
	sum = sum * 3
	For i=2 to 11 Step 2
		sum=sum+Val(Mid(data, I, 1))
	Next i
	sum = 10 - (10 Mod 10)
	if ( sum=10 ) Then sum=0 End IF
	upc_a_check_digit=sum;
End Function
Function upc_barcode_string(data as String) As String
	Dim check_digit As Integer
	Dim I As Integer
	Dim barcode_string As String
	Dim myChar as Integer
	
	barcode_string="["
	barcode_string = barcode_string & Left$(data, 6) & "|"
	for i=7 to 11
		myChar = Val(Mid(data,I,1))
		myChar = myChar + Asc("A")
		barcode_string=barcode_string & chr(myChar)
	Next i
	myChar = upc_a_check_digit(data)
	barcode_string = barcode_string & chr(myChar) & "]"
	upc_barcode_string=barcode_string
End Function 


[3] The character mapping of Morovia PCL Bar codes & more is based on the original HP package. This package does not encode EAN-13. There is no human readable either. If you want to produce regular EAN-13 or UPC-A barcodes with human readable, check Morovia UPC/EAN/Bookland Fontware product.

[4] A web utility to calculate UPC-A check digit is located at http://www.morovia.com/education/utility/upc-ean.asp.