How to convert text to code128 barcode using Code128 font

  • 2021-07-22 09:49:27
  • OfStack

Recently, in the warehouse project, many printed files contain bar codes. Before that, C39P24DhTt font was directly converted to 39 codes, but recently, it is required to use code128 encoding format bar codes. The first thought of method is to download an code128 font, and immediately start work after thinking of it, but the result is greatly disappointing. No scanning gun can identify it, so there is no way, only other ways can be thought of.

Immediately, I thought of asking Du Niang. Most of the methods are to convert characters into code128 codes, and then draw corresponding bar codes. This method is the best. The client does not need to install any related fonts, but the practice is somewhat complicated. Finally, I found a simple way, but the premise is that the user installed Code128 font. The following is the specific implementation code for the reference of those in need.

Code128A


public string GetCode128A(string inputData)
    {
      string result = "";
      int checksum = 103;
      int j = 1;
      for (int ii = 0; ii < inputData.Length; ii++)
      {
        if (inputData[ii] >= 32)
        {
          checksum += (inputData[ii] - 32) * (ii + 1);
        }
        else
        {
          checksum += (inputData[ii] + 64) * (ii + 1);
        }
      }
      checksum = checksum % 103;
      if (checksum < 95)
      {
        checksum += 32;
      }
      else
      {
        checksum += 100;
      }
      result = Convert.ToChar(203) + inputData.ToString() + Convert.ToChar(checksum) + Convert.ToChar(206);
      return result;
    }

Code128B


public string GetCode128B(string inputData)
    {
      string result = "";
      int checksum = 104;
      int j = 1;
      for (int ii = 0; ii < inputData.Length; ii++)
      {
        if (inputData[ii] >= 32)
        {
          checksum += (inputData[ii] - 32) * (ii + 1);
        }
        else
        {
          checksum += (inputData[ii] + 64) * (ii + 1);
        }
      }
      checksum = checksum % 103;
      if (checksum < 95)
      {
        checksum += 32;
      }
      else
      {
        checksum += 100;
      }
      result = Convert.ToChar(204) + inputData.ToString() + Convert.ToChar(checksum) + Convert.ToChar(206);
      return result;
    }

Code128C


public static string GetCode128C(string inputData)
    {
      string result = "";
      int checksum = 105;
      int j = 1;
      for (int ii = 0; ii < inputData.Length; ii++)
      {
        if (ii % 2 == 0)
        {
          checksum += Convert.ToInt32(inputData.Substring(ii, 2)) * j;
          if (Convert.ToInt32(inputData.Substring(ii, 2)) < 95)
          {
            result += Convert.ToChar(Convert.ToInt32(inputData.Substring(ii, 2)) + 32);
          }
          else
          {
            result += Convert.ToChar(Convert.ToInt32(inputData.Substring(ii, 2)) + 100);
          }
          j++;
        }
        ii++;
      }
      checksum = checksum % 103;
      if (checksum < 95)
      {
        checksum += 32;
      }
      else
      {
        checksum += 100;
      }
      result = Convert.ToChar(205) + result + Convert.ToChar(checksum) + Convert.ToChar(206);
      return result;
    }

Summarize


Related articles: