/// <summary> /// Determine if a character can be represented in a given codeset /// </summary> /// <param name="CharAscii">character to check for</param> /// <param name="currcs">codeset context to test</param> /// <returns>true if the codeset contains a representation for the ASCII character</returns> public static bool CharCompatibleWithCodeset(int CharAscii, CodeSet currcs) { CodeSetAllowed csa = CodesetAllowedForChar(CharAscii); return(csa == CodeSetAllowed.CodeAorB || (csa == CodeSetAllowed.CodeA && currcs == CodeSet.CodeA) || (csa == CodeSetAllowed.CodeB && currcs == 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 static CodeSet GetBestStartSet(CodeSetAllowed csa1, CodeSetAllowed csa2) { int vote = 0; vote += (csa1 == CodeSetAllowed.CodeA) ? 1 : 0; vote += (csa1 == CodeSetAllowed.CodeB) ? -1 : 0; vote += (csa2 == CodeSetAllowed.CodeA) ? 1 : 0; vote += (csa2 == 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 static int[] StringToCode128(string AsciiData) { // turn the string into ascii byte data byte[] asciiBytes = Encoding.ASCII.GetBytes(AsciiData); // decide which codeset to start with CodeSetAllowed csa1 = asciiBytes.Length > 0 ? CodesetAllowedForChar(asciiBytes[0]) : CodeSetAllowed.CodeAorB; CodeSetAllowed csa2 = asciiBytes.Length > 0 ? CodesetAllowedForChar(asciiBytes[1]) : CodeSetAllowed.CodeAorB; CodeSet currcs = GetBestStartSet(csa1, csa2); // set up the beginning of the barcode System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3); // assume no codeset changes, account for start, checksum, and stop codes.Add(StartCodeForCodeSet(currcs)); // 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(CodesForChar(thischar, nextchar, ref currcs)); } // 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(StopCode()); int[] result = codes.ToArray(typeof(int)) as int[]; return(result); }
public static bool CharCompatibleWithCodeset(int CharAscii, CodeSet currcs) { CodeSetAllowed allowed = CodesetAllowedForChar(CharAscii); return(((allowed == CodeSetAllowed.CodeAorB) || ((allowed == CodeSetAllowed.CodeA) && (currcs == CodeSet.CodeA))) || ((allowed == CodeSetAllowed.CodeB) && (currcs == CodeSet.CodeB))); }
private CodeSet PickStartSet(CodeSetAllowed csa1, CodeSetAllowed csa2) { int vote = 0; vote += (csa1 == CodeSetAllowed.CodeA) ? 1 : 0; vote += (csa1 == CodeSetAllowed.CodeB) ? -1 : 0; vote += (csa2 == CodeSetAllowed.CodeA) ? 1 : 0; vote += (csa2 == CodeSetAllowed.CodeB) ? -1 : 0; return (vote > 0) ? CodeSet.CodeA : CodeSet.CodeB; }