如何将 UPC-E 条码转换为 UPC-A 条码?
How to convert a UPC-E barcode to a UPC-A barcode?
将零抑制的八位 GTIN-12 标识符(表示为 UPC-E 条形码)转换为 UPC-A 条形码所示的完整十二位版本的算法是什么?
从以下模式映射中可以最清楚地看出在 UPC-E 和 UPC-A 表示之间转换 GTIN-12 标识符的算法:
SabcdeNX ⟺ SabN0000cdeX : 0≤N≤2
Sabcde3X ⟺ Sabc00000deX
Sabcde4X ⟺ Sabcd00000eX
SabcdeNX ⟺ Sabcde0000NX : 5≤N≤9
上面S
是数制(0或1),X
是校验位
在伪代码中它看起来像这样:
Input: A valid eight-digit UPC-E: Assigned to E[].
Output: PASS: Twelve-digit UPC-A representing the UPC-E.
FAIL: Reason.
if E[0] != {0-1} then FAIL: Invalid number system.
if E[6] == {0-2} then PASS: E[0..2] . E[6] . "0000" . E[3..5] . E[7]
if E[6] == "3" then PASS: E[0..3] . "00000" . E[4..5] . E[7]
if E[6] == "4" then PASS: E[0..4] . "00000" . E[5] . E[7]
if E[6] == {5-9} then PASS: E[0..5] . "0000" . E[6] . E[7]
这重复了@Terry Burton 的答案中的算法,写在Java。
switch (data.charAt(6)) {
case '0':
case '1':
case '2': {
data = data.substring(0, 3) + "0000" + data.charAt(6) + data.substring(3, 6) + data.charAt(7);
break;
}
case '3': {
data = data.substring(0, 4) + "00000" + data.substring(4, 5) + data.charAt(7);
break;
}
case '4': {
data = data.substring(0, 5) + "00000" + data.charAt(5) + data.charAt(7);
break;
}
case '5':
case '6':
case '7':
case '8':
case '9': {
data = data.substring(0, 6) + "0000" + data.charAt(6) + data.charAt(7);
break;
}
}
Java 代码在 0-2 的情况下作为错误发布。这是已更正的 C# 版本。以及一些测试用例。
public string Expand_UPCE_to_UPCA_GTIN12(string upce)
{
return upce[6] switch
{
'0' or '1' or '2' => $"{upce[..3]}{upce[6]}0000{upce[3..6]}{upce[7]}",
'3' => $"{upce[..4]}00000{upce[4..6]}{upce[7]}",
'4' => $"{upce[..5]}00000{upce[5]}{upce[7]}",
_ => $"{upce[..6]}0000{upce[6..]}",
};
}
Console.WriteLine($"Test A: {Expand_UPCE_to_UPCA_GTIN12("02345673")} becomes 023456000073");
Console.WriteLine($"Test B: {Expand_UPCE_to_UPCA_GTIN12("02345147")} becomes 023450000017");
Console.WriteLine($"Test C: {Expand_UPCE_to_UPCA_GTIN12("08679339")} becomes 086700000939");
Console.WriteLine($"Test D: {Expand_UPCE_to_UPCA_GTIN12("06397126")} becomes 063200009716");
Console.WriteLine($"Test E: {Expand_UPCE_to_UPCA_GTIN12("07832309")} becomes 078000003239"); // Diet Dr Pepper 12 fl oz can
在 Swift 5 中回答。请注意,此 returns 0
如果无法进行转换,因此如果您不想要该结果,则需要更改代码。包含扩展以使 String
索引范围更易于使用:
static func convertUPCEtoUPCA(code: String) -> String {
if code.isNumber && code.length == 8
{
switch code[6] {
case "0", "1", "2":
return "\(code.prefix(3))\(String(code[6]))0000\(code[3..<6])\(String(code[7]))"
case "3":
return "\(code.prefix(4))00000\(code[4..<6])\(String(code[7]))"
case "4":
return "\(code.prefix(5))00000\(String(code[5]))\(String(code[7]))"
case "5", "6", "7", "8", "9":
return "\(code.prefix(6))0000\(code.suffix(2))"
default:
return "0"
}
} else {
return "0"
}
}
extension String {
subscript(_ range: CountableRange<Int>) -> String {
let start = index(startIndex, offsetBy: max(0, range.lowerBound))
let end = index(start, offsetBy: min(self.count - range.lowerBound,
range.upperBound - range.lowerBound))
return String(self[start..<end])
}
subscript(_ range: CountablePartialRangeFrom<Int>) -> String {
let start = index(startIndex, offsetBy: max(0, range.lowerBound))
return String(self[start...])
}
}
extension StringProtocol {
subscript(offset: Int) -> Character {
self[index(startIndex, offsetBy: offset)]
}
}
将零抑制的八位 GTIN-12 标识符(表示为 UPC-E 条形码)转换为 UPC-A 条形码所示的完整十二位版本的算法是什么?
从以下模式映射中可以最清楚地看出在 UPC-E 和 UPC-A 表示之间转换 GTIN-12 标识符的算法:
SabcdeNX ⟺ SabN0000cdeX : 0≤N≤2
Sabcde3X ⟺ Sabc00000deX
Sabcde4X ⟺ Sabcd00000eX
SabcdeNX ⟺ Sabcde0000NX : 5≤N≤9
上面S
是数制(0或1),X
是校验位
在伪代码中它看起来像这样:
Input: A valid eight-digit UPC-E: Assigned to E[].
Output: PASS: Twelve-digit UPC-A representing the UPC-E.
FAIL: Reason.
if E[0] != {0-1} then FAIL: Invalid number system.
if E[6] == {0-2} then PASS: E[0..2] . E[6] . "0000" . E[3..5] . E[7]
if E[6] == "3" then PASS: E[0..3] . "00000" . E[4..5] . E[7]
if E[6] == "4" then PASS: E[0..4] . "00000" . E[5] . E[7]
if E[6] == {5-9} then PASS: E[0..5] . "0000" . E[6] . E[7]
这重复了@Terry Burton 的答案中的算法,写在Java。
switch (data.charAt(6)) {
case '0':
case '1':
case '2': {
data = data.substring(0, 3) + "0000" + data.charAt(6) + data.substring(3, 6) + data.charAt(7);
break;
}
case '3': {
data = data.substring(0, 4) + "00000" + data.substring(4, 5) + data.charAt(7);
break;
}
case '4': {
data = data.substring(0, 5) + "00000" + data.charAt(5) + data.charAt(7);
break;
}
case '5':
case '6':
case '7':
case '8':
case '9': {
data = data.substring(0, 6) + "0000" + data.charAt(6) + data.charAt(7);
break;
}
}
Java 代码在 0-2 的情况下作为错误发布。这是已更正的 C# 版本。以及一些测试用例。
public string Expand_UPCE_to_UPCA_GTIN12(string upce)
{
return upce[6] switch
{
'0' or '1' or '2' => $"{upce[..3]}{upce[6]}0000{upce[3..6]}{upce[7]}",
'3' => $"{upce[..4]}00000{upce[4..6]}{upce[7]}",
'4' => $"{upce[..5]}00000{upce[5]}{upce[7]}",
_ => $"{upce[..6]}0000{upce[6..]}",
};
}
Console.WriteLine($"Test A: {Expand_UPCE_to_UPCA_GTIN12("02345673")} becomes 023456000073");
Console.WriteLine($"Test B: {Expand_UPCE_to_UPCA_GTIN12("02345147")} becomes 023450000017");
Console.WriteLine($"Test C: {Expand_UPCE_to_UPCA_GTIN12("08679339")} becomes 086700000939");
Console.WriteLine($"Test D: {Expand_UPCE_to_UPCA_GTIN12("06397126")} becomes 063200009716");
Console.WriteLine($"Test E: {Expand_UPCE_to_UPCA_GTIN12("07832309")} becomes 078000003239"); // Diet Dr Pepper 12 fl oz can
在 Swift 5 中回答。请注意,此 returns 0
如果无法进行转换,因此如果您不想要该结果,则需要更改代码。包含扩展以使 String
索引范围更易于使用:
static func convertUPCEtoUPCA(code: String) -> String {
if code.isNumber && code.length == 8
{
switch code[6] {
case "0", "1", "2":
return "\(code.prefix(3))\(String(code[6]))0000\(code[3..<6])\(String(code[7]))"
case "3":
return "\(code.prefix(4))00000\(code[4..<6])\(String(code[7]))"
case "4":
return "\(code.prefix(5))00000\(String(code[5]))\(String(code[7]))"
case "5", "6", "7", "8", "9":
return "\(code.prefix(6))0000\(code.suffix(2))"
default:
return "0"
}
} else {
return "0"
}
}
extension String {
subscript(_ range: CountableRange<Int>) -> String {
let start = index(startIndex, offsetBy: max(0, range.lowerBound))
let end = index(start, offsetBy: min(self.count - range.lowerBound,
range.upperBound - range.lowerBound))
return String(self[start..<end])
}
subscript(_ range: CountablePartialRangeFrom<Int>) -> String {
let start = index(startIndex, offsetBy: max(0, range.lowerBound))
return String(self[start...])
}
}
extension StringProtocol {
subscript(offset: Int) -> Character {
self[index(startIndex, offsetBy: offset)]
}
}