C # code generation (4) -- Code128Auto

Source: Internet
Author: User

Code128Auto is the minimum length of code written in full compliance with the GBT 18347-2001 128 bar code above.

/// <Summary> /// Code128Auto bar code, following the minimum length principle // </summary> public class Code128Auto: absCode128 {public Code128Auto (string rawData): base (rawData) {} protected override bool RawDataCheck () {// the ASCII code range of Code128 is 0 ~ 127 foreach (char c in this. _ rawData) {if (byte) c> 127) {return false ;}} return true;} protected override string GetEncodedData () {StringBuilder tempBuilder = new StringBuilder (); characterSet nowCharacterSet = Code128.GetCharacterSet (this. _ rawData, 0); int checkNum; // check character switch (nowCharacterSet) {case CharacterSet. a: tempBuilder. append (Code128.BSList [Code128.StartA]); // Add the start character StartA checkN Um = Code128.StartA; break; case CharacterSet. b: tempBuilder. append (Code128.BSList [Code128.StartB]); // Add the start character StartB checkNum = Code128.StartB; break; default: tempBuilder. append (Code128.BSList [Code128.StartC]); // Add the start character StartC checkNum = Code128.StartC; break;} int nowWeight = 1, nowIndex = 0; this. getEncodedData (tempBuilder, nowCharacterSet, ref nowIndex, ref nowWeight, ref checkNum); checkNum %= 103; TempBuilder. append (Code128.BSList [checkNum]); // Add the verification character tempBuilder. append (Code128.BSList [Code128.Stop]); // Add the terminator return tempBuilder. toString ();} /// <summary> /// general method /// </summary> /// <param name = "tempBuilder"> </param> /// <param name = "sIndex"> </param> // <param name = "nowWeight"> </param> // <param name = "checkNum"> </param> private void encodingCommon (StringBuilder tempBuilder, byte sIndex, ref Int nowWeight, ref int checkNum) {tempBuilder. append (Code128.BSList [sIndex]); checkNum + = nowWeight * sIndex; nowWeight ++ ;} /// <summary> /// get the encoded data /// </summary> /// <param name = "tempBuilder"> encode the data container </param>/ // <param name = "nowCharacterSet"> Current Character Set </param> // <param name = "I"> string index </param> /// <param name = "nowWeight"> current weight </param> /// <param name = "checkNum"> sum of current checked values </param> private void GetEnco DedData (StringBuilder tempBuilder, CharacterSet nowCharacterSet, ref int I, ref int nowWeight, ref int checkNum) {// because character set C may exist, there may be inconsistency between I and nowWeight, therefore, we need to define byte sIndex; switch (nowCharacterSet) {case CharacterSet. a: case CharacterSet. b: for (; I <this. _ rawData. length; I ++) {if (char. isDigit (this. _ rawData [I]) {// number int digitLength = Code128.GetDigitLength (this. _ rawData, I); if (digitLength> = 4) {// transfer to CodeC if (digitLength % 2! = 0) {// odd digit number. After the first digit, insert the CodeC character sIndex = Code128.GetSIndex (nowCharacterSet, (this. _ rawData [I]); this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); I ++;} nowCharacterSet = CharacterSet. c; sIndex = Code128.GetCodeXIndex (nowCharacterSet); // Insert the CodeC switching character this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); this. getEncodedData (tempBuilder, nowCharacterSet, ref I, re F nowWeight, ref checkNum); return;} else {// if it is less than four digits, the internal loop ends directly for (int j = 0; j <digitLength; j ++) {sIndex = Code128.GetSIndex (nowCharacterSet, (this. _ rawData [I]); this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); I ++;} I --; // After the above loop ends, it will cause I to add 1 more, so we need to subtract 1 continue;} else if (code128.charnotbelonsto (nowCharacterSet, this. _ rawData [I]) {// The current character does not belong to the Current Character Set byte tempByte = Code128.GetProprietaryChar (this. _ rawData, I + 1); // obtain the character set CharacterSet tempCharacterSet = character (nowCharacterSet) that belongs to a or B after the current character. if (tempByte! = Byte. maxValue & code128.charonlybelonsto (nowCharacterSet, (char) tempByte) {// Add the conversion character sIndex = Code128.SHIFT _ AB; this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); sIndex = Code128.GetSIndex (tempCharacterSet, this. _ rawData [I]); this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); continue;} else {// Add the switch operator nowCharacterSet = tempCharacterSet; sIndex = Code128. GetCodeXIndex (nowCharacterSet); this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); this. getEncodedData (tempBuilder, nowCharacterSet, ref I, ref nowWeight, ref checkNum); return ;}} else {sIndex = Code128.GetSIndex (nowCharacterSet, this. _ rawData [I]); this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum) ;}} break; default: for (; I <this. _ rawData. length; I + = 2) {if (I! = This. _ rawData. length-1 & char. isDigit (this. _ rawData, I) & char. isDigit (this. _ rawData, I + 1) {sIndex = byte. parse (this. _ rawData. substring (I, 2); this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum);} else {nowCharacterSet = Code128.GetCharacterSet (this. _ rawData, I); // Insert the conversion character sIndex = Code128.GetCodeXIndex (nowCharacterSet); this. encodingCommon (tempBuilder, sIndex, ref now Weight, ref checkNum); this. getEncodedData (tempBuilder, nowCharacterSet, ref I, ref nowWeight, ref checkNum); return ;}} break ;}# region logout/private void GetEncodedData (StringBuilder tempBuilder, string data, characterSet nowCharacterSet, ref int nowWeight, ref int checkNum) // {// byte sIndex, nowChar; // switch (nowCharacterSet) // {// case CharacterSet. a: // for (int I = 0; I <data. lengt H; I ++) // {// nowChar = (byte) data [I]; // if (nowChar> 95) /// {// Character Set B // byte tempByte = Code128.GetProprietaryChar (data, I + 1); // if (tempByte <32) // {// Add the conversion character // sIndex = Code128.SHIFT _ AB; // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // sIndex = Code128.GetSIndexFromB (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum );// Continue; //} // else // {// Add the toggle // sIndex = Code128.CODEB _ AC; // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // this. getEncodedData (tempBuilder, data. substring (I), CharacterSet. b, ref nowWeight, ref checkNum); // return; //} // else if (char. isDigit (data [I]) // {// number // int digitLength = Code128.GetDigitLength (data, I); // if (digitLength> = 4) //{// // Transfer to CodeC // if (digitLength % 2! = 0) // {// odd digit number, insert the CodeC character after the first digit // sIndex = Code128.GetSIndexFromA (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // I ++; //} // sIndex = Code128.CODEC _ AB; // Insert the CodeC switching character // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // this. getEncodedData (tempBuilder, data. substring (I), CharacterSet. c, ref nowWeight, ref checkNum); // return ;//} // Else // {// for (int j = 0; j <digitLength; j ++) // {// sIndex = Code128.GetSIndexFromA (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); I ++; //} I --; // continue; ///} // sIndex = Code128.GetSIndexFromA (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // break; // case CharacterSet. b: // for (int I = 0; I <data. Length; I ++) // {// nowChar = (byte) data [I]; // if (nowChar <32) /// {// character set A // byte tempByte = Code128.GetProprietaryChar (data, I + 1); // if (tempByte> 95) // {// Add the conversion character // sIndex = Code128.SHIFT _ AB; // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // sIndex = Code128.GetSIndexFromA (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum ); // Continue; // else // {// Add the toggle // sIndex = Code128.CODEA _ BC; // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // this. getEncodedData (tempBuilder, data. substring (I), CharacterSet. b, ref nowWeight, ref checkNum); // return; //} // else if (char. isDigit (data [I]) // {// number // int digitLength = Code128.GetDigitLength (data, I); // if (digitLength> = 4 )// {// Transfer to CodeC // if (digitLength % 2! = 0) // {// odd digit number, insert the CodeC character after the first digit // sIndex = Code128.GetSIndexFromB (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // I ++; //} // sIndex = Code128.CODEC _ AB; // Insert the CodeC switching character // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // this. getEncodedData (tempBuilder, data. substring (I), CharacterSet. c, ref nowWeight, ref checkNum); // return ;//} // Else // {// for (int j = 0; j <digitLength; j ++) // {// sIndex = Code128.GetSIndexFromB (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); I ++; //} I --; // continue; ///} // sIndex = Code128.GetSIndexFromB (data [I]); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); //} // break; // default: // for (int I = 0; I <data. length; I + = 2) // {// if (I! = Data. length-1 & char. isDigit (data, I) & char. isDigit (data, I + 1) // {// sIndex = byte. parse (data. substring (I, 2); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); //} // else // {// nowCharacterSet = Code128.GetCharacterSet (data, I ); //// Insert the conversion character // sIndex = Code128.GetCodeXIndex (nowCharacterSet); // this. encodingCommon (tempBuilder, sIndex, ref nowWeight, ref checkNum); // this. getEncodedData (tempBuilder, data. substring (I), nowCharacterSet, ref nowWeight, ref checkNum); // return; //} // break; //} # endregion}

The logout part is the first way to get the encoding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.