private int[] CreateAsciiForDigit(string AsciiData) { Int16 val1, val2; //if (AsciiData.Length % 2 != 2) AsciiData = "0" + AsciiData; System.Collections.ArrayList outData = new System.Collections.ArrayList(); char[] StringArray = AsciiData.ToCharArray(); for (int i = 0; i < StringArray.Length; i++) { Int16.TryParse(StringArray[i++].ToString(), out val1); try { Int16.TryParse(StringArray[i].ToString(), out val2); } catch (IndexOutOfRangeException) { val2 = 0; CodeSet temp = CodeSet.CodeB;//Code128Code.CodesetForChar(val1); val2 = (short)Code128Code.CodeValueForChar(val1, CodeSet.CodeC); outData.Add((int)Code128Code.ShiftCodeForCodeSet(temp)); outData.Add((int)val2);; return(outData.ToArray(typeof(int)) as int[]); } val1 *= 10; val1 += val2; outData.Add((int)val1); } return(outData.ToArray(typeof(int)) as int[]); }
/// <summary> /// Determines the best starting code set based on the the first two /// characters of the string to be encoded /// </summary> /// <param name="csa1">First character of input string</param> /// <param name="csa2">Second character of input string</param> /// <returns>The codeset determined to be best to start with</returns> private CodeSet GetBestStartSet(Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2) { var vote = 0; vote += csa1 == Code128Code.CodeSetAllowed.CodeA ? 1 : 0; vote += csa1 == Code128Code.CodeSetAllowed.CodeB ? -1 : 0; vote += csa2 == Code128Code.CodeSetAllowed.CodeA ? 1 : 0; vote += csa2 == Code128Code.CodeSetAllowed.CodeB ? -1 : 0; return vote > 0 ? CodeSet.CodeA : CodeSet.CodeB; // ties go to codeB due to my own prejudices }
/// <summary> /// Transform the string into integers representing the Code128 codes /// necessary to represent it /// </summary> /// <param name="AsciiData">String to be encoded</param> /// <returns>Code128 representation</returns> private int[] StringToCode128(string AsciiData) { byte[] asciiBytes = null; System.Collections.ArrayList codes = new System.Collections.ArrayList(); // turn the string into ascii byte data Int64 temp; if (Int64.TryParse(AsciiData, out temp)) { codes.Add(105); codes.AddRange(CreateAsciiForDigit(AsciiData)); } else { asciiBytes = Encoding.ASCII.GetBytes(AsciiData); // decide which codeset to start with Code128Code.CodeSetAllowed csa1 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[0]) : Code128Code.CodeSetAllowed.CodeA; Code128Code.CodeSetAllowed csa2 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[1]) : Code128Code.CodeSetAllowed.CodeA; CodeSet currcs = GetBestStartSet(csa1, csa2); // set up the beginning of the barcode codes = new System.Collections.ArrayList(asciiBytes.Length + 3); // assume no codeset changes, account for start, checksum, and stop codes.Add(Code128Code.StartCodeForCodeSet(currcs)); //codes.Add(104); // add the codes for each character in the string for (int i = 0; i < asciiBytes.Length; i++) { int thischar = asciiBytes[i]; int nextchar = asciiBytes.Length > (i + 1) ? asciiBytes[i + 1] : -1; codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currcs, ref i)); } } // calculate the check digit int checksum = (int)(codes[0]); for (int i = 1; i < codes.Count; i++) { checksum += i * (int)(codes[i]); } codes.Add(checksum % 103); codes.Add(Code128Code.StopCode()); int[] result = codes.ToArray(typeof(int)) as int[]; return(result); }
/// <summary> /// Transform the string into integers representing the Code128 codes /// necessary to represent it /// </summary> /// <param name="asciiData">String to be encoded</param> /// <returns>Code128 representation</returns> private int[] StringToCode128(string asciiData) { // turn the string into ascii byte data var asciiBytes = Encoding.ASCII.GetBytes(asciiData); // decide which codeset to start with var csa1 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[0]) : Code128Code.CodeSetAllowed.CodeAorB; var csa2 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[1]) : Code128Code.CodeSetAllowed.CodeAorB; var currentCodeSet = this.GetBestStartSet(csa1, csa2); // set up the beginning of the barcode // assume no codeset changes, account for start, checksum, and stop var codes = new ArrayList(asciiBytes.Length + 3) { Code128Code.StartCodeForCodeSet(currentCodeSet) }; // add the codes for each character in the string for (var i = 0; i < asciiBytes.Length; i++) { int thischar = asciiBytes[i]; var nextchar = asciiBytes.Length > i + 1 ? asciiBytes[i + 1] : -1; codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currentCodeSet)); } // calculate the check digit var checksum = (int)codes[0]; for (var i = 1; i < codes.Count; i++) { checksum += i * (int)codes[i]; } codes.Add(checksum % 103); codes.Add(Code128Code.StopCode()); var result = codes.ToArray(typeof(int)) as int[]; return(result); }
/// <summary> /// Determines the best starting code set based on the the first two /// characters of the string to be encoded /// </summary> /// <param name="csa1">First character of input string</param> /// <param name="csa2">Second character of input string</param> /// <returns>The codeset determined to be best to start with</returns> private CodeSet GetBestStartSet( Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2 ) { int vote = 0; vote += (csa1==Code128Code.CodeSetAllowed.CodeA) ? 1 : 0; vote += (csa1==Code128Code.CodeSetAllowed.CodeB) ? -1 : 0; vote += (csa2==Code128Code.CodeSetAllowed.CodeA) ? 1 : 0; vote += (csa2==Code128Code.CodeSetAllowed.CodeB) ? -1 : 0; return (vote>0) ? CodeSet.CodeA : CodeSet.CodeB; // ties go to codeB due to my own prejudices }