private string CalculateCheckDigit() { string currentStartChar = _FormattedData[0]; uint CheckSum = 0; for (uint i = 0; i < _FormattedData.Count; i++) { //replace apostrophes with double apostrophes for escape chars string s = _FormattedData[(int)i].Replace("'", "''"); //try to find value in the A column Code128Struct rows = GetCode128ValueByA(s); //try to find value in the B column if (null == rows) { rows = GetCode128ValueByB(s); } //try to find value in the C column if (null == rows) { rows = GetCode128ValueByC(s); } uint value = UInt32.Parse(rows["Value"].ToString()); uint addition = value * ((i == 0) ? 1 : i); CheckSum += addition; }//for uint Remainder = (CheckSum % 103); Code128Struct RetRows = GetCode128ByValue(Remainder.ToString()); return(RetRows.Encoding); }
private string GetEncoding() { //break up data for encoding BreakUpDataForEncoding(); //insert the start characters InsertStartandCodeCharacters(); string CheckDigit = CalculateCheckDigit(); string Encoded_Data = ""; foreach (string s in _FormattedData) { //handle exception with apostrophes in select statements string s1 = s.Replace("'", "''"); Code128Struct E_Row = GetCode128ValueByA(s1); if (E_Row == null) { E_Row = GetCode128ValueByB(s1); if (null == E_Row) { E_Row = GetCode128ValueByC(s1); } //if } //if if (null == E_Row) { throw new Exception("EC128-3: Could not find encoding of a value( " + s1 + " ) in the formatted data."); } Encoded_Data += E_Row["Encoding"].ToString(); _EncodedData.Add(E_Row["Encoding"].ToString()); }//foreach //add the check digit Encoded_Data += CalculateCheckDigit(); _EncodedData.Add(CalculateCheckDigit()); //add the stop character Encoded_Data += GetCode128ValueByA("STOP")["Encoding"].ToString(); _EncodedData.Add(GetCode128ValueByA("STOP")["Encoding"].ToString()); //add the termination bars Encoded_Data += "11"; _EncodedData.Add("11"); return(Encoded_Data); }
private void InsertStartandCodeCharacters() { Code128Struct CurrentCodeSet = null; string CurrentCodeString = ""; if (this.type != TYPES.DYNAMIC) { switch (this.type) { case TYPES.A: _FormattedData.Insert(0, "START_A"); break; case TYPES.B: _FormattedData.Insert(0, "START_B"); break; case TYPES.C: _FormattedData.Insert(0, "START_C"); break; default: throw new Exception("EC128-4: Unknown start type in fixed type encoding."); } }//if else { try { for (int i = 0; i < (_FormattedData.Count); i++) { int col = 0; List <Code128Struct> tempStartChars = FindStartorCodeCharacter(_FormattedData[i], ref col); if (null == tempStartChars) { return; } //check all the start characters and see if we need to stay with the same codeset or if a change of sets is required bool sameCodeSet = false; foreach (Code128Struct row in tempStartChars) { if (row["A"].ToString().EndsWith(CurrentCodeString) || row["B"].ToString().EndsWith(CurrentCodeString) || row["C"].ToString().EndsWith(CurrentCodeString)) { sameCodeSet = true; break; } //if } //foreach //only insert a new code char if starting a new codeset //if (CurrentCodeString == "" || !tempStartChars[0][col].ToString().EndsWith(CurrentCodeString)) /* Removed because of bug */ if (CurrentCodeString == "" || !sameCodeSet) { CurrentCodeSet = tempStartChars[0]; if (null == CurrentCodeSet) { return; } bool error = true; while (error) { try { CurrentCodeString = CurrentCodeSet[col].ToString().Split(new char[] { '_' })[1]; error = false; }//try catch { error = true; if (col++ > CurrentCodeSet.GetType().GetProperties().Length) { throw new Exception("No start character found in CurrentCodeSet."); } } //catch } //while _FormattedData.Insert(i++, CurrentCodeSet[col].ToString()); } //if } //for } //try catch (Exception ex) { throw new Exception("EC128-3: Could not insert start and code characters.\n Message: " + ex.Message); } //catch } //else }
private List <Code128Struct> FindStartorCodeCharacter(string s, ref int col) { List <Code128Struct> rows = new List <Code128Struct>(); //if two chars are numbers then START_C or CODE_C if (s.Length > 1 && Char.IsNumber(s[0]) && Char.IsNumber(s[1])) { if (StartCharacter == null) { StartCharacter = GetCode128ValueByA("START_C"); if (null != StartCharacter) { rows.Add(StartCharacter); } }//if else { Code128Struct tmp = GetCode128ValueByA("CODE_C"); if (null != tmp) { rows.Add(tmp); } } col = 1; }//if else { bool AFound = false; bool BFound = false; foreach (Code128Struct row in this.C128_Code) { try { if (!AFound && s == row["A"].ToString()) { AFound = true; col = 2; if (StartCharacter == null) { StartCharacter = GetCode128ValueByA("START_A"); rows.Add(StartCharacter); }//if else { rows.Add(GetCode128ValueByB("CODE_A"));//first column is FNC4 so use B }//else }//if else if (!BFound && s == row["B"].ToString()) { BFound = true; col = 1; if (StartCharacter == null) { StartCharacter = GetCode128ValueByA("START_B"); rows.Add(StartCharacter); }//if else { rows.Add(GetCode128ValueByA("CODE_B")); } }//else else if (AFound && BFound) { break; } }//try catch (Exception ex) { throw new Exception("EC128-1: " + ex.Message); } //catch } //foreach if (rows.Count <= 0) { throw new Exception("EC128-2: Could not determine start character."); } }//else return(rows); }