ITF条形码算法

时间:2014-12-04 18:38:51

标签: c# barcode

我有这个C#ITF/Interleaved 2 of 5算法(来自VB Azalea Software 算法):

public static string Interleaved25(string input)
{
    if (input.Length <= 0) return "";

    if (input.Length % 2 != 0)
    {
        input = "0" + input;
    }

    string result = "";

    //Takes pairs of numbers and convert them to chars
    for (int i = 0; i <= input.Length - 1; i += 2)
    {
        int pair = Int32.Parse(input.Substring(i, 2));

        if (pair < 90) 
            pair = pair + 33;
        else if (pair == 90)
            pair = pair + 182;
       else if (pair == 91)
            pair = pair + 183;
       else if (pair > 91)
            pair = pair + 104;

       result = result + Convert.ToChar(pair);
   }

   //Leading and trailing chars.
   return (char)171 + result + (char)172;
}

问题在于chars value > 89 {{1}}所有结果都是空框(使用ITF字体)。

3 个答案:

答案 0 :(得分:1)

我知道这是一个老问题。但这是一个带校验和的解决方案:

using System;

public class Program
{
    public static void Main()
    {       
        var bcTxt = "0000420876801";//"29902110013"; //"0000420876801";
        int prod1 = 0;
            int prod2 = 0;
            string Txt1 = "";
            string Txt2 = "";

            Txt1 = bcTxt;
            // Berechnen der Prüfziffer, wenn ungerade Anzahl von Zeichen
            if (Txt1.Length % 2 == 1)
            {
                for (int i = 0; i < Txt1.Length; i++)
                {
                    prod1 += int.Parse(Txt1.Substring(i, 1)) * 3;
                    if (i < Txt1.Length -1)
                    {
                        prod1 += int.Parse(Txt1.Substring(i + 1, 1));
                        i += 1;                 
                    }
                    Console.WriteLine(prod1);
                }
                Console.WriteLine(prod1);
                prod2 = prod1 % 10;
                Console.WriteLine(prod2);
                if (prod2 == 0) 
                {
                    prod2 = 10;
                }
                prod2 = 10 - prod2;                                 
                Txt1 += (char)(prod2 + 48);
                Console.WriteLine(Txt1);
            }
            //Ascii Zeichen zuordnen
            //beim Code 2/5 werden die Zeichen paarweise zugeordnet
            Txt2 = ((char)34).ToString();
            string Tmp = "";
            for (int i = 0; i < Txt1.Length; i++)
            {
                Tmp += Txt1.Substring(i, 2);
                i += 1;
                if (int.Parse(Tmp) > 91)
                {
                    Txt2 += ((char)(int.Parse(Tmp) + 70)).ToString();
                }
                else
                {
                    Txt2 += ((char)(int.Parse(Tmp) + 36)).ToString();
                }
                Tmp = "";
            }

            Txt2 += ((char)35).ToString();
        Console.WriteLine(Txt2);
    }
}

答案 1 :(得分:0)

在输入这个问题时,我自己得到了答案。

这是新代码:

if (pair < 90)
    pair = pair + 33;
else
    {
        pair = pair + 71;
    }

基本上,90到99之间的所有chars都需要+ 71。

New Data

答案 2 :(得分:-1)

vb6中的相同代码

Public Function DevolverI2of5(ByVal cString As String) as String
Dim i As Integer

If Len(cString) Mod 2 <> 0 Then
    cString = "0" & cString
End If

Dim result As String

'Takes pairs of numbers and convert them to chars
For i = 1 To Len(cString) Step 2
    Dim pair As Integer
    pair = Val(Mid(cString, i, 2))

    If pair < 90 Then
        pair = pair + 33
    Else
        pair = pair + 71
    End If

   result = result & Chr(pair)

Next i

DevolverI2of5 = Chr(171) & result & Chr(172)