private string GetNextChar(string code, ref int index, ref HCCode128Encoding encoding) { int vLen = code.Length - 1; if (index > vLen) { return(""); } string vResult = "", vC = ""; if ((code[index] == '&') && (index + 2 <= vLen) && (code[index + 2] == ';')) { vC = code[index + 1].ToString().ToUpper(); if ((vC == "A") || (vC == "B") || (vC == "C") || (vC == "S") || (vC == "1") || (vC == "2") || (vC == "3") || (vC == "4")) { index += 3; vResult = "&" + vC + ";"; return(vResult); } } if ((encoding == HCCode128Encoding.encC) && (index + 1 <= vLen)) { vResult = code.Substring(index, 2); index += 2; return(vResult); } vResult = code.Substring(index, 1); index++; return(vResult); }
private string Encode(string code) { code = this.StripControlCodes(code, false); string vResult = ""; int vIndex = 0; HCCode128Encoding vEncoding = HCCode128Encoding.encNone; int vLen = code.Length; while (vIndex < vLen) { vResult = vResult + this.GetNextPortion(code, ref vIndex, ref vEncoding); } return(vResult); }
private string StripControlCodes(string code, bool stripFNCodes) { string vResult = ""; int vIndex = 0, vLen = code.Length; HCCode128Encoding vEncoding = HCCode128Encoding.encNone; string vNextChar = ""; while (vIndex < vLen) { vNextChar = this.GetNextChar(code, ref vIndex, ref vEncoding); if ((vNextChar != "&A;") && (vNextChar != "&B;") && (vNextChar != "&C;") && (vNextChar != "&S;")) { if ((!stripFNCodes) || ((vNextChar != "&1;") && (vNextChar != "&2;") && (vNextChar != "&3;") && (vNextChar != "&4;"))) { vResult = vResult + vNextChar; } } } return(vResult); }
private string GetNextPortion(string code, ref int aindex, ref HCCode128Encoding aencoding) { int vLen = code.Length; if (aindex > vLen - 1) { return(""); } int vIndexa, vIndexb, numDigits, numChars; HCCode128Encoding firstCharEncoding, nextCharEncoding; string prefix, vC = "", vResult = ""; if ((code[aindex] == '&') && (aindex + 2 < vLen) && (code[aindex + 2] == ';')) { vC = code[aindex + 1].ToString().ToUpper(); if ((vC == "A") || (vC == "B") || (vC == "C") || (vC == "S") || (vC == "1") || (vC == "2") || (vC == "3") || (vC == "4")) { vC = code.Substring(aindex, 3); aindex += 3; } else { vC = ""; } } vIndexa = this.FindCodeA(code[aindex].ToString()); vIndexb = this.FindCodeB(code[aindex].ToString()); firstCharEncoding = HCCode128Encoding.encA; if ((vIndexa == -1) && (vIndexb != -1)) { firstCharEncoding = HCCode128Encoding.encB; } else if ((vIndexa != -1) && (vIndexb != -1)) { firstCharEncoding = HCCode128Encoding.encAorB; } numDigits = 0; if (this.IsFourOrMoreDigits(code, aindex, ref numDigits)) { firstCharEncoding = HCCode128Encoding.encC; } if (firstCharEncoding == HCCode128Encoding.encC) { numDigits = (numDigits / 2) * 2; vResult = code.Substring(aindex, numDigits); aindex += numDigits; if (aencoding != HCCode128Encoding.encC) { vResult = "&C;" + vC + vResult; } else { vResult = vC + vResult; } aencoding = HCCode128Encoding.encC; return(vResult); } numChars = 1; while (aindex + numChars < vLen) { vIndexa = this.FindCodeA(code[aindex + numChars].ToString()); vIndexb = this.FindCodeB(code[aindex + numChars].ToString()); nextCharEncoding = HCCode128Encoding.encA; if ((vIndexa == -1) && (vIndexb != -1)) { nextCharEncoding = HCCode128Encoding.encB; } else if ((vIndexa != -1) && (vIndexb != -1)) { nextCharEncoding = HCCode128Encoding.encAorB; } if (this.IsFourOrMoreDigits(code, aindex + numChars, ref numDigits)) { nextCharEncoding = HCCode128Encoding.encC; } if ((nextCharEncoding != HCCode128Encoding.encC) && (nextCharEncoding != firstCharEncoding)) { if (firstCharEncoding == HCCode128Encoding.encAorB) { firstCharEncoding = nextCharEncoding; } else if (nextCharEncoding == HCCode128Encoding.encAorB) { nextCharEncoding = firstCharEncoding; } } if (firstCharEncoding != nextCharEncoding) { break; } numChars++; } if (firstCharEncoding == HCCode128Encoding.encAorB) { firstCharEncoding = HCCode128Encoding.encB; } if (firstCharEncoding == HCCode128Encoding.encA) { prefix = "&A;"; } else { prefix = "&B;"; } if ((aencoding != firstCharEncoding) && (numChars == 1) && ((aencoding == HCCode128Encoding.encA) || (aencoding == HCCode128Encoding.encB)) && ((firstCharEncoding == HCCode128Encoding.encA) || (firstCharEncoding == HCCode128Encoding.encB))) { prefix = "&S;"; } else { aencoding = firstCharEncoding; } vResult = prefix + vC + code.Substring(aindex, numChars); aindex += numChars; return(vResult); }
private string GetCode(string text) { text = text.Replace("&FNC1;", "&1;"); text = this.Encode(text); HCCode128Encoding vEncoding = HCCode128Encoding.encNone; int vIndex = 0, vChecksum, vCodewordPos, vIdx; string nextChar = this.GetNextChar(text, ref vIndex, ref vEncoding); string startCode = ""; if (nextChar == "&A;") { vEncoding = HCCode128Encoding.encA; vChecksum = 103; startCode = Table128[103, 3]; } else if (nextChar == "&B;") { vEncoding = HCCode128Encoding.encB; vChecksum = 104; startCode = Table128[104, 3]; } else if (nextChar == "&C;") { vEncoding = HCCode128Encoding.encC; vChecksum = 105; startCode = Table128[105, 3]; } else { throw new Exception("无效的条码内容!"); } string vResult = startCode; vCodewordPos = 1; int vLen = text.Length; while (vIndex < vLen) { nextChar = this.GetNextChar(text, ref vIndex, ref vEncoding); if (nextChar == "&A;") { vEncoding = HCCode128Encoding.encA; vIdx = 101; } else if (nextChar == "&B;") { vEncoding = HCCode128Encoding.encB; vIdx = 100; } else if (nextChar == "&C;") { vEncoding = HCCode128Encoding.encC; vIdx = 99; } else if (nextChar == "&S;") { if (vEncoding == HCCode128Encoding.encA) { vEncoding = HCCode128Encoding.encB; } else { vEncoding = HCCode128Encoding.encA; } vIdx = 98; } else if (nextChar == "&1;") { vIdx = 102; } else if (nextChar == "&2;") { vIdx = 97; } else if (nextChar == "&3;") { vIdx = 96; } else if (nextChar == "&4;") { if (vEncoding == HCCode128Encoding.encA) { vIdx = 101; } else { vIdx = 100; } } else { if (vEncoding == HCCode128Encoding.encA) { vIdx = this.FindCodeA(nextChar[0].ToString()); } else if (vEncoding == HCCode128Encoding.encB) { vIdx = this.FindCodeB(nextChar[0].ToString()); } else { vIdx = this.FindCodeC(nextChar); } } if (vIdx < 0) { throw new Exception("无效的条码内容!"); } vResult = vResult + Table128[vIdx, 3]; vChecksum += vIdx * vCodewordPos; vCodewordPos++; if (nextChar == "&S;") { if (vEncoding == HCCode128Encoding.encA) { vEncoding = HCCode128Encoding.encB; } else { vEncoding = HCCode128Encoding.encA; } } } vChecksum = vChecksum % 103; vResult = vResult + Table128[vChecksum, 3]; vResult = vResult + "2331112"; vResult = this.Convert(vResult); return(vResult); }