iBhavik
iBhavik

Reputation: 647

How to convert UPC-A to UPC-E?

We would like to convert the 12 digit UPC-A to 8 digit UPC-E. Can you tell me which is the best way to do this without having to use my own code to convert?

I got many formula for convert the 8 digit UCC-E to 12 digit UPC-A but not reverse.

Upvotes: 3

Views: 3151

Answers (2)

Terry Burton
Terry Burton

Reputation: 3029

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

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

In the above S is the number system, either 0 or 1 and X is the check digit. If a UPC-A doesn't match a pattern then it cannot be converted to UPC-E.

It can be seen that there may be up to four valid UPC-E representations of each UPC-A:

001200000067 ⟺ 00100627 ⟺ 00120637 ⟺ 00120647 ⟺ 00120067.

Pseudo-code performing one method of conversion from UPC-A to UPC-E looks like this:

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

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

if A[3] == {0-2} && A[4..7] == "0000"  then PASS: A[0..2] . A[8..10] . A[3]  . A[11]
if A[4..8] == "00000"                  then PASS: A[0..3] . A[9..10] . "3"   . A[11]
if A[5..9] == "00000"                  then PASS: A[0..4] . A[10]    . "4"   . A[11]
if A[6..9] == "0000" && A[10] == {5-9} then PASS: A[0..5] .            A[10] . A[11]

otherwise, FAIL: UPC-A not compatible with UPC-E.

Upvotes: 4

Hiren Panchal
Hiren Panchal

Reputation: 3023

            NSString *strScannedCode = @"028200002921";
        NSString *strBarCodeType = @"UPC E";

        NSString *strAlteredScannedCode = strScannedCode;

        if ([strBarCodeType isEqualToString:@"UPC E"])
        {
            if (strScannedCode.length == 12)
            {
                NSString *strManufacturerCode = [strScannedCode substringWithRange:(NSMakeRange(1, 5))];
                NSString *strProductCode = [strScannedCode substringWithRange:NSMakeRange(6, 5)];

                NSLog(@"strManufacturerCode = %@",strManufacturerCode);
                NSLog(@"strProductCode = %@",strProductCode);

                if ([[strManufacturerCode substringWithRange:NSMakeRange(2, 3)] isEqualToString:@"000"] ||
                    [[strManufacturerCode substringWithRange:NSMakeRange(2, 3)] isEqualToString:@"100"] ||
                    [[strManufacturerCode substringWithRange:NSMakeRange(2, 3)] isEqualToString:@"200"])
                {
                    strAlteredScannedCode = STRING(@"%@%@%@",[strManufacturerCode substringWithRange:NSMakeRange(0, 2)],
                                                   [strProductCode substringWithRange:NSMakeRange(2, 3)],
                                                   [strManufacturerCode substringWithRange:NSMakeRange(2, 1)]);
                }
                else if ([[strManufacturerCode substringWithRange:NSMakeRange(3, 2)] isEqualToString:@"00"])
                {
                    strAlteredScannedCode = STRING(@"%@%@3",[strManufacturerCode substringWithRange:NSMakeRange(0, 3)],
                                                   [strProductCode substringWithRange:NSMakeRange(3, 2)]);

                }
                else if ([strManufacturerCode characterAtIndex:4] == '0')
                {
                    strAlteredScannedCode = STRING(@"%@%@4",[strManufacturerCode substringWithRange:NSMakeRange(0, 4)],
                                                   [strProductCode substringWithRange:NSMakeRange(4, 1)]);
                }
                else if ([strManufacturerCode characterAtIndex:4] != '0')
                {
                    strAlteredScannedCode = STRING(@"%@%@",strManufacturerCode,
                                                   [strProductCode substringWithRange:NSMakeRange(4, 1)]);
                }

                strAlteredScannedCode = STRING(@"%@%@%@",[strScannedCode substringWithRange:NSMakeRange(0, 1)],strAlteredScannedCode,[strScannedCode substringWithRange:NSMakeRange(11, 1)]);

                NSLog(@"strUPC_E_Code = %@",strAlteredScannedCode);
            }
        }

By implementing above code you will get 12 digit to 8 digit, For Example you will get result as "02829221", This is UPC E of "028200002921".

Upvotes: 1

Related Questions