Nicke Manarin
Nicke Manarin

Reputation: 3358

ITF Barcode Algorithm

I have this C# ITF/Interleaved 2 of 5 algorithm (from a VB Azalea Software algorithm):

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;
}

The problem is that somehow chars with value > 89 all have empty boxes as result (using the ITF font).

Upvotes: 0

Views: 940

Answers (3)

Leo
Leo

Reputation: 11

same code in 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)

Upvotes: 0

Uke
Uke

Reputation: 174

I know this is an old question. But here is a solution with checksum:

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);
    }
}

Upvotes: 1

Nicke Manarin
Nicke Manarin

Reputation: 3358

While typing this question, I got the answer all by myself.

Here is the new code:

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

Basically, all chars between 90 and 99 needs a + 71.

New Data

Upvotes: 0

Related Questions