private int[] StringToCode128(string AsciiData) { int num; byte[] bytes = Encoding.ASCII.GetBytes(AsciiData); Code128Code.CodeSetAllowed allowed = (bytes.Length > 0) ? Code128Code.CodesetAllowedForChar(bytes[0]) : Code128Code.CodeSetAllowed.CodeAorB; Code128Code.CodeSetAllowed allowed2 = (bytes.Length > 0) ? Code128Code.CodesetAllowedForChar(bytes[1]) : Code128Code.CodeSetAllowed.CodeAorB; CodeSet bestStartSet = this.GetBestStartSet(allowed, allowed2); ArrayList list = new ArrayList(bytes.Length + 3); list.Add(Code128Code.StartCodeForCodeSet(bestStartSet)); for (num = 0; num < bytes.Length; num++) { int charAscii = bytes[num]; int lookAheadAscii = (bytes.Length > (num + 1)) ? bytes[num + 1] : -1; list.AddRange(Code128Code.CodesForChar(charAscii, lookAheadAscii, ref bestStartSet)); } int num4 = (int)list[0]; for (num = 1; num < list.Count; num++) { num4 += num * ((int)list[num]); } list.Add(num4 % 0x67); list.Add(Code128Code.StopCode()); return(list.ToArray(typeof(int)) as int[]); }
private int[] StringToCode128(string AsciiData) { byte[] asciiBytes = Encoding.ASCII.GetBytes(AsciiData); Code128Code.CodeSetAllowed csa1 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[0]) : Code128Code.CodeSetAllowed.CodeAorB; Code128Code.CodeSetAllowed csa2 = asciiBytes.Length > 1 ? Code128Code.CodesetAllowedForChar(asciiBytes[1]) : Code128Code.CodeSetAllowed.CodeAorB; CodeSet currcs = GetBestStartSet(csa1, csa2); System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3); // assume no codeset changes, account for start, checksum, and stop codes.Add(Code128Code.StartCodeForCodeSet(currcs)); 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)); } 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); }
private CodeSet GetBestStartSet(Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2) { int num = 0; num += (csa1 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0; num += (csa1 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0; num += (csa2 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0; num += (csa2 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0; return((num > 0) ? CodeSet.CodeA : CodeSet.CodeB); }
/// <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 }
/// <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); }
private int GetCodeSetWeight(Code128Code.CodeSetAllowed code) { switch (code) { case Code128Code.CodeSetAllowed.CodeA: return(1); case Code128Code.CodeSetAllowed.CodeB: return(2); case Code128Code.CodeSetAllowed.CodeC: return(4); default: return(2); } }
/// <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 += this.GetCodeSetWeight(csa1); vote += this.GetCodeSetWeight(csa2); switch (vote) { case 8: return(CodeSet.CodeC); case 2: case 5: return(CodeSet.CodeA); default: return(CodeSet.CodeB); } }
/// <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 byte[] asciiBytes = Encoding.ASCII.GetBytes(AsciiData); // decide which codeset to start with Code128Code.CodeSetAllowed csa1 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[0]) : Code128Code.CodeSetAllowed.CodeAorB; Code128Code.CodeSetAllowed csa2 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[1]) : Code128Code.CodeSetAllowed.CodeAorB; CodeSet currcs = GetBestStartSet(csa1, csa2); // set up the beginning of the barcode var codes = new ArrayList(asciiBytes.Length + 3) { Code128Code.StartCodeForCodeSet(currcs) }; // assume no codeset changes, account for start, checksum, and stop // 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)); } // calculate the check digit var 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()); var result = codes.ToArray(typeof(int)) as int[]; return(result); }