How to convert UPC-A to UPC-E?
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);
}
}
When you implement the above code, you will get between 12 digits and 8 digits. For example, you get the result as "02829221", this is UPC E "028200002921".
source to share
The algorithm for converting a GTIN-12 identifier between UPC-A and UPC-E representation can be most clearly seen from the following template display:
SabN0000cdeX ⟺ SabcdeNX : 0≤N≤2 Sabc00000deX ⟺ Sabcde3X Sabcd00000eX ⟺ Sabcde4X Sabcde0000NX ⟺ SabcdeNX : 5≤N≤9
The above S
has a number system, either 0 or 1, and X
is a check digit. If UPC-A does not match the pattern, it cannot be converted to UPC-E.
You can see that there can be up to four valid UPC-E representations of each UPC-A:
001200000067 ⟺ 00100627 ⟺ 00120637 ⟺ 00120647 ⟺ 00120067.
The pseudocode that performs one of the conversion methods 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] FAIL: UPC-A not compatible with UPC-E.
source to share