如何将 UPC-A 转换为 UPC-E?
How to convert UPC-A to UPC-E?
我们想将 12 位 UPC-A 转换为 8 位 UPC-E。你能告诉我最好的方法是不用我自己的代码来转换吗?
我有很多公式可以将 8 位 UCC-E 转换为 12 位 UPC-A 但不能反转。
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".
从以下模式映射中可以最清楚地看出在 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。
It can be seen that there may be up to four valid UPC-E representations of each UPC-A:
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]
otherwise, FAIL: UPC-A not compatible with UPC-E.
我们想将 12 位 UPC-A 转换为 8 位 UPC-E。你能告诉我最好的方法是不用我自己的代码来转换吗?
我有很多公式可以将 8 位 UCC-E 转换为 12 位 UPC-A 但不能反转。
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".
从以下模式映射中可以最清楚地看出在 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。
It can be seen that there may be up to four valid UPC-E representations of each UPC-A:
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]
otherwise, FAIL: UPC-A not compatible with UPC-E.