Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a UPC-E barcode to a UPC-A barcode?

Tags:

barcode

What is the algorithm for converting a zero-suppressed, eight-digit GTIN-12 identifier (represented as a UPC-E barcode) into the full, twelve-digit version as shown in a UPC-A barcode?

like image 717
Terry Burton Avatar asked Jul 21 '15 12:07

Terry Burton


People also ask

What is the difference between UPC-A and UPC-E?

Differences Between UPC-A and UPC-E UPC-E is also called zero suppressed UPC because UPC-E compresses a normal twelve-digit UPC-A number into a six-digit code by suppressing the number system digit, trailing zeros in the manufacturers code and leading zeros in the product identification part of the bar code message.

What is UPC conversion?

A UPC-A code consists of 11 digits for the leading, single-digit product code, manufacturer's code, item number and a check digit. Written verbally a UPC-A code becomes (LPC)(MC)(IN)(CD). The manufacturer's code contains up to three trailing zeros, and the item number contains up to four leading zeros.

Does UPC-E have a check digit?

UPC-E encodes six digits of numeric message data together with a number system digit and a check digit, for a total of eight digits. Most barcode symbologies print bars and spaces in only two widths, but with UPC, four different widths are used.


2 Answers

The algorithm for converting a GTIN-12 identifier between UPC-E and UPC-A representation can be most clearly seen from the following pattern mapping:

SabcdeNX ⟺ SabN0000cdeX  :  0≤N≤2
Sabcde3X ⟺ Sabc00000deX
Sabcde4X ⟺ Sabcd00000eX
SabcdeNX ⟺ Sabcde0000NX  :  5≤N≤9

In the above S is the number system (either 0 or 1) and X is the check digit.

In pseudo-code it looks like this:

Input:  A valid eight-digit UPC-E: Assigned to E[]. 
Output: PASS: Twelve-digit UPC-A representing the UPC-E.
        FAIL: Reason.

if E[0] != {0-1} then FAIL: Invalid number system.

if E[6] == {0-2} then PASS: E[0..2] . E[6] . "0000"  . E[3..5] . E[7]
if E[6] == "3"   then PASS: E[0..3] .        "00000" . E[4..5] . E[7]
if E[6] == "4"   then PASS: E[0..4] .        "00000" . E[5]    . E[7]    
if E[6] == {5-9} then PASS: E[0..5] .        "0000"  . E[6]    . E[7]
like image 117
Terry Burton Avatar answered Sep 26 '22 20:09

Terry Burton


This duplicates the algorithm in @Terry Burton's answer, written in Java.


switch (data.charAt(6)) {
    case '0':
    case '1':
    case '2': {
        data = data.substring(0, 3) + data.charAt(6) + "0000" + data.substring(3, 6) + data.charAt(7);
        break;
    }
    case '3': {
        data = data.substring(0, 4) + "00000" + data.substring(4, 6) + data.charAt(7);
        break;
    }
    case '4': {
        data = data.substring(0, 5) + "00000" + data.charAt(5) + data.charAt(7);
        break;
    }
    case '5':
    case '6':
    case '7':
    case '8':
    case '9': {
        data = data.substring(0, 6) + "0000" + data.charAt(6) + data.charAt(7);
        break;
    }
}
like image 25
psyklopz Avatar answered Sep 24 '22 20:09

psyklopz