iBhavik iBhavik - 1 month ago 6
iOS Question

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.

Answer
            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".

Comments