/// <summary> /// Encode the raw data using the JAN-13 algorithm. /// </summary> private string Encode_JAN13() { if (!Raw_Data.StartsWith("49")) { throw new System.Exception("EJAN13-1: Invalid Country Code for JAN13 (49 required)"); } if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EJAN13-2: Numeric Data Only"); } EAN13 ean13 = new EAN13(Raw_Data); return(ean13.Encoded_Value); }//Encode_JAN13
}//Standard2of5 /// <summary> /// Encode the raw data using the Standard 2 of 5 algorithm. /// </summary> private string Encode_Standard2of5() { if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("ES25-1: Numeric Data Only"); } string result = "11011010"; foreach (char c in Raw_Data) { result += S25_Code[Int32.Parse(c.ToString())]; }//foreach //add ending bars result += "1101011"; return(result); }//Encode_Standard2of5
/// <summary> /// Encode the raw data using the UPC Supplemental 2-digit algorithm. /// </summary> private string Encode_UPCSupplemental_2() { if (Raw_Data.Length != 2) { throw new System.Exception("EUPC-SUP2-1: Invalid data length. (Length = 2 required)"); } if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EUPC-SUP2-2: Numeric Data Only"); } string pattern = ""; try { pattern = this.UPC_SUPP_2[Int32.Parse(Raw_Data.Trim()) % 4]; }//try catch { throw new System.Exception("EUPC-SUP2-3: Invalid Data. (Numeric only)"); } string result = "1011"; int pos = 0; foreach (char c in pattern) { if (c == 'a') { //encode using odd parity result += EAN_CodeA[Int32.Parse(Raw_Data[pos].ToString())]; }//if else if (c == 'b') { //encode using even parity result += EAN_CodeB[Int32.Parse(Raw_Data[pos].ToString())]; }//else if if (pos++ == 0) { result += "01"; //Inter-character separator } }//foreach return(result); }//Encode_UPSSupplemental_2
/// <summary> /// Encode the raw data using the Bookland/ISBN algorithm. /// </summary> private string Encode_ISBN_Bookland() { if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EBOOKLANDISBN-1: Numeric Data Only"); } string type = "UNKNOWN"; if (Raw_Data.Length == 10 || Raw_Data.Length == 9) { if (Raw_Data.Length == 10) { Raw_Data = Raw_Data.Remove(9, 1); } Raw_Data = "978" + Raw_Data; type = "ISBN"; }//if else if (Raw_Data.Length == 12 && Raw_Data.StartsWith("978")) { type = "BOOKLAND-NOCHECKDIGIT"; }//else if else if (Raw_Data.Length == 13 && Raw_Data.StartsWith("978")) { type = "BOOKLAND-CHECKDIGIT"; Raw_Data = Raw_Data.Remove(12, 1); }//else if //check to see if its an unknown type if (type == "UNKNOWN") { throw new System.Exception("EBOOKLANDISBN-2: Invalid input. Must start with 978 and be length must be 9, 10, 12, 13 characters."); } EAN13 ean13 = new EAN13(Raw_Data); return(ean13.Encoded_Value); }//Encode_ISBN_Bookland
/// <summary> /// Encode the raw data using the EAN-8 algorithm. /// </summary> private string Encode_EAN8() { //check length if (Raw_Data.Length != 8 && Raw_Data.Length != 7) { throw new System.Exception("EEAN8-1: Invalid data length. (7 or 8 numbers only)"); } //check numeric only if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EEAN8-2: Numeric only."); } //encode the data string result = "101"; //first half (Encoded using left hand / odd parity) for (int i = 0; i < Raw_Data.Length / 2; i++) { result += EAN_CodeA[Int32.Parse(Raw_Data[i].ToString())]; }//for //center guard bars result += "01010"; //second half (Encoded using right hand / even parity) for (int i = Raw_Data.Length / 2; i < Raw_Data.Length; i++) { result += EAN_CodeC[Int32.Parse(Raw_Data[i].ToString())]; }//for result += "101"; return(result); }//Encode_EAN8
/// <summary> /// Encode the raw data using the UPC-A algorithm. /// </summary> private string Encode_UPCA() { //check length of input if (Raw_Data.Length != 12) { throw new System.Exception("EUPCA-1: Data length invalid. (Length must be 12)"); } if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EUPCA-2: Numeric Data Only"); } string result = "101"; //start with guard bars string patterncode = UPC_Pattern[Int32.Parse(Raw_Data[0].ToString())]; //first number result += UPC_CodeA[Int32.Parse(Raw_Data[0].ToString())]; //second (group) of numbers int pos = 0; while (pos < 5) { if (patterncode[pos + 1] == 'a') { result += UPC_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())]; } if (patterncode[pos + 1] == 'b') { result += UPC_CodeB[Int32.Parse(Raw_Data[pos + 1].ToString())]; } pos++; }//while //add divider bars result += "01010"; //third (group) of numbers pos = 0; while (pos < 5) { result += UPC_CodeC[Int32.Parse(Raw_Data[(pos++) + 6].ToString())]; }//while //forth result += UPC_CodeC[Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString())]; //add ending guard bars result += "101"; //get the manufacturer assigning country this.init_CountryCodes(); string twodigitCode = "0" + Raw_Data.Substring(0, 1); try { _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString(); }//try catch { throw new System.Exception("EUPCA-3: Country assigning manufacturer code not found."); }//catch finally { CountryCodes.Clear(); } return(result); }//Encode_UPCA
/// <summary> /// Encode the raw data using the EAN-13 algorithm. (Can include the checksum already. If it doesnt exist in the data then it will calculate it for you. Accepted data lengths are 12 + 1 checksum or just the 12 data digits) /// </summary> private string Encode_EAN13() { //check length of input if (Raw_Data.Length < 12 || Raw_Data.Length > 13) { throw new System.Exception("EEAN13-1: Data length invalid. (Length must be 12 or 13)"); } if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EEAN13-2: Numeric Data Only"); } string patterncode = EAN_Pattern[Int32.Parse(Raw_Data[0].ToString())]; string result = "101"; //first //result += EAN_CodeA[Int32.Parse(RawData[0].ToString())]; //second int pos = 0; while (pos < 6) { if (patterncode[pos] == 'a') { result += EAN_CodeA[Int32.Parse(Raw_Data[pos + 1].ToString())]; } if (patterncode[pos] == 'b') { result += EAN_CodeB[Int32.Parse(Raw_Data[pos + 1].ToString())]; } pos++; }//while //add divider bars result += "01010"; //get the third pos = 1; while (pos <= 5) { result += EAN_CodeC[Int32.Parse(Raw_Data[(pos++) + 6].ToString())]; }//while //checksum digit int cs = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString()); //add checksum result += EAN_CodeC[cs]; //add ending bars result += "101"; //get the manufacturer assigning country init_CountryCodes(); _Country_Assigning_Manufacturer_Code = "N/A"; string twodigitCode = Raw_Data.Substring(0, 2); string threedigitCode = Raw_Data.Substring(0, 3); try { _Country_Assigning_Manufacturer_Code = CountryCodes[threedigitCode].ToString(); }//try catch { try { _Country_Assigning_Manufacturer_Code = CountryCodes[twodigitCode].ToString(); }//try catch { throw new System.Exception("EEAN13-3: Country assigning manufacturer code not found."); } //catch } //catch finally { CountryCodes.Clear(); } return(result); }//Encode_EAN13
}//MSI /// <summary> /// Encode the raw data using the MSI algorithm. /// </summary> private string Encode_MSI() { //check for non-numeric chars if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EMSI-1: Numeric Data Only"); } string PreEncoded = Raw_Data; //get checksum if (Encoded_Type == EncodingType.MSI_Mod10 || Encoded_Type == EncodingType.MSI_2Mod10) { string odds = ""; string evens = ""; for (int i = PreEncoded.Length - 1; i >= 0; i -= 2) { odds = PreEncoded[i].ToString() + odds; if (i - 1 >= 0) { evens = PreEncoded[i - 1].ToString() + evens; } }//for //multiply odds by 2 odds = Convert.ToString((Int32.Parse(odds) * 2)); int evensum = 0; int oddsum = 0; foreach (char c in evens) { evensum += Int32.Parse(c.ToString()); } foreach (char c in odds) { oddsum += Int32.Parse(c.ToString()); } int checksum = 10 - ((oddsum + evensum) % 10); PreEncoded += checksum.ToString(); }//if if (Encoded_Type == EncodingType.MSI_Mod11 || Encoded_Type == EncodingType.MSI_Mod11_Mod10) { int sum = 0; int weight = 2; for (int i = PreEncoded.Length - 1; i >= 0; i--) { if (weight > 7) { weight = 2; } sum += Int32.Parse(PreEncoded[i].ToString()) * weight++; }//foreach int checksum = 11 - (sum % 11); PreEncoded += checksum.ToString(); }//else if (Encoded_Type == EncodingType.MSI_2Mod10 || Encoded_Type == EncodingType.MSI_Mod11_Mod10) { //get second check digit if 2 mod 10 was selected or Mod11/Mod10 string odds = ""; string evens = ""; for (int i = PreEncoded.Length - 1; i >= 0; i -= 2) { odds = PreEncoded[i].ToString() + odds; if (i - 1 >= 0) { evens = PreEncoded[i - 1].ToString() + evens; } }//for //multiply odds by 2 odds = Convert.ToString((Int32.Parse(odds) * 2)); int evensum = 0; int oddsum = 0; foreach (char c in evens) { evensum += Int32.Parse(c.ToString()); } foreach (char c in odds) { oddsum += Int32.Parse(c.ToString()); } int checksum = 10 - ((oddsum + evensum) % 10); PreEncoded += checksum.ToString(); }//if string result = "110"; foreach (char c in PreEncoded) { result += MSI_Code[Int32.Parse(c.ToString())]; }//foreach //add stop character result += "1001"; return(result); }//Encode_MSI
/// <summary> /// Encode the raw data using the UPC Supplemental 5-digit algorithm. /// </summary> private string Encode_UPCSupplemental_5() { if (Raw_Data.Length != 5) { throw new System.Exception("EUPC-SUP5-1: Invalid data length. (Length = 5 required)."); } if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EUPCA-2: Numeric Data Only"); } //calculate the checksum digit int even = 0; int odd = 0; //odd for (int i = 0; i <= 4; i += 2) { odd += Int32.Parse(Raw_Data.Substring(i, 1)) * 3; }//for //even for (int i = 1; i < 4; i += 2) { even += Int32.Parse(Raw_Data.Substring(i, 1)) * 9; }//for int total = even + odd; int cs = total % 10; string pattern = UPC_SUPP_5[cs]; string result = ""; int pos = 0; foreach (char c in pattern) { //Inter-character separator if (pos == 0) { result += "1011"; } else { result += "01"; } if (c == 'a') { //encode using odd parity result += EAN_CodeA[Int32.Parse(Raw_Data[pos].ToString())]; }//if else if (c == 'b') { //encode using even parity result += EAN_CodeB[Int32.Parse(Raw_Data[pos].ToString())]; } //else if pos++; } //foreach return(result); } //Encode_UPCSupplemental_5
}//UPCE /// <summary> /// Encode the raw data using the UPC-E algorithm. /// </summary> private string Encode_UPCE() { if (Raw_Data.Length != 6 && Raw_Data.Length != 8 && Raw_Data.Length != 12) { throw new System.Exception("EUPCE-1: Invalid data length. (8 or 12 numbers only)"); } if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EUPCE-2: Numeric only."); } int CheckDigit = Int32.Parse(Raw_Data[Raw_Data.Length - 1].ToString()); int NumberSystem = Int32.Parse(Raw_Data[0].ToString()); //Convert to UPC-E from UPC-A if necessary if (Raw_Data.Length == 12) { string UPCECode = ""; //break apart into components string Manufacturer = Raw_Data.Substring(1, 5); string ProductCode = Raw_Data.Substring(6, 5); //check for a valid number system if (NumberSystem != 0 && NumberSystem != 1) { throw new System.Exception("EUPCE-3: Invalid Number System (only 0 & 1 are valid)"); } if (Manufacturer.EndsWith("000") || Manufacturer.EndsWith("100") || Manufacturer.EndsWith("200") && Int32.Parse(ProductCode) <= 999) { //rule 1 UPCECode += Manufacturer.Substring(0, 2); //first two of manufacturer UPCECode += ProductCode.Substring(2, 3); //last three of product UPCECode += Manufacturer[2].ToString(); //third of manufacturer }//if else if (Manufacturer.EndsWith("00") && Int32.Parse(ProductCode) <= 99) { //rule 2 UPCECode += Manufacturer.Substring(0, 3); //first three of manufacturer UPCECode += ProductCode.Substring(3, 2); //last two of product UPCECode += "3"; //number 3 }//else if else if (Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9) { //rule 3 UPCECode += Manufacturer.Substring(0, 4); //first four of manufacturer UPCECode += ProductCode[4]; //last digit of product UPCECode += "4"; //number 4 }//else if else if (!Manufacturer.EndsWith("0") && Int32.Parse(ProductCode) <= 9 && Int32.Parse(ProductCode) >= 5) { //rule 4 UPCECode += Manufacturer; //manufacturer UPCECode += ProductCode[4]; //last digit of product }//else if else { throw new System.Exception("EUPCE-4: Illegal UPC-A entered for conversion. Unable to convert."); } Raw_Data = UPCECode; }//if //get encoding pattern string pattern = ""; if (NumberSystem == 0) { pattern = UPCE_Code_0[CheckDigit]; } else { pattern = UPCE_Code_1[CheckDigit]; } //encode the data string result = "101"; int pos = 0; foreach (char c in pattern) { int i = Int32.Parse(Raw_Data[pos++].ToString()); if (c == 'a') { result += EAN_CodeA[i]; }//if else if (c == 'b') { result += EAN_CodeB[i]; } //else if } //foreach //guard bars result += "01010"; //end bars result += "1"; return(result); }//Encode_UPCE
}//Code11 /// <summary> /// Encode the raw data using the Code 11 algorithm. /// </summary> private string Encode_Code11() { if (!Barcode.CheckNumericOnly(Raw_Data.Replace("-", ""))) { throw new System.Exception("EC11-1: Numeric data and '-' Only"); } //calculate the checksums int weight = 1; int CTotal = 0; string Data_To_Encode_with_Checksums = Raw_Data; //figure the C checksum for (int i = Raw_Data.Length - 1; i >= 0; i--) { //C checksum weights go 1-10 if (weight == 10) { weight = 1; } if (Raw_Data[i] != '-') { CTotal += Int32.Parse(Raw_Data[i].ToString()) * weight++; } else { CTotal += 10 * weight++; } }//for int checksumC = CTotal % 11; Data_To_Encode_with_Checksums += checksumC.ToString(); //K checksums are recommended on any message length greater than or equal to 10 if (Raw_Data.Length >= 1) { weight = 1; int KTotal = 0; //calculate K checksum for (int i = Data_To_Encode_with_Checksums.Length - 1; i >= 0; i--) { //K checksum weights go 1-9 if (weight == 9) { weight = 1; } if (Data_To_Encode_with_Checksums[i] != '-') { KTotal += Int32.Parse(Data_To_Encode_with_Checksums[i].ToString()) * weight++; } else { KTotal += 10 * weight++; } }//for int checksumK = KTotal % 11; Data_To_Encode_with_Checksums += checksumK.ToString(); }//if //encode data string space = "0"; string result = C11_Code[11] + space; //start-stop char + interchar space foreach (char c in Data_To_Encode_with_Checksums) { int index = (c == '-' ? 10 : Int32.Parse(c.ToString())); result += C11_Code[index]; //inter-character space result += space; }//foreach //stop bars result += C11_Code[11]; return(result); }//Encode_Code11
/// <summary> /// Encode the raw data using the ITF-14 algorithm. /// </summary> private string Encode_ITF14() { //check length of input if (Raw_Data.Length > 14 || Raw_Data.Length < 13) { throw new System.Exception("EITF14-1: Data length invalid. (Length must be 13 or 14)"); } if (!Barcode.CheckNumericOnly(Raw_Data)) { throw new System.Exception("EITF14-2: Numeric data only."); } string result = "1010"; for (int i = 0; i < Raw_Data.Length; i += 2) { bool bars = true; string patternbars = ITF14_Code[Int32.Parse(Raw_Data[i].ToString())]; string patternspaces = ITF14_Code[Int32.Parse(Raw_Data[i + 1].ToString())]; string patternmixed = ""; //interleave while (patternbars.Length > 0) { patternmixed += patternbars[0].ToString() + patternspaces[0].ToString(); patternbars = patternbars.Substring(1); patternspaces = patternspaces.Substring(1); }//while foreach (char c1 in patternmixed) { if (bars) { if (c1 == 'N') { result += "1"; } else { result += "11"; } }//if else { if (c1 == 'N') { result += "0"; } else { result += "00"; } } //else bars = !bars; } //foreach } //foreach //add ending bars result += "1101"; return(result); }//Encode_ITF14