如何将UPC-A转换为UPC-E?

时间:2015-06-26 13:05:47

标签: ios objective-c barcode

我们希望将12位UPC-A转换为8位UPC-E。你能不能用我自己的代码转换来告诉我哪种方法最好?

我有很多公式可以将8位UCC-E转换为12位UPC-A而不是反转。

2 个答案:

答案 0 :(得分:3)

从以下模式映射中可以最清楚地看到用于在UPC-A和UPC-E表示之间转换GTIN-12标识符的算法:

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

在上面S是数字系统,0或1,X是校验位。如果UPC-A与模式不匹配,则无法将其转换为UPC-E。

  

可以看出,每个UPC-A最多可能有四个有效的UPC-E表示:

     

001200000067⟺00100627⟺00120637⟺00120647⟺00120067。

执行从UPC-A到UPC-E的一种转换方法的伪代码如下所示:

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.

答案 1 :(得分:1)

            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);
            }
        }
  

通过实现上述代码,您将获得12位数到8位数,例如,您将得到“02829221”的结果,这是“028200002921”的UPC E.