Link to home
Start Free TrialLog in
Avatar of tomcahill
tomcahillFlag for Canada

asked on

Need help converting a VB Script into C# application

I need to generate code 128 barcodes.  I am using a code 128 barcode font for the printing and with that I need to generate the check character.  The barcode format is (Start Char) + Data + (Check Char) + (End Char).

I have a VB script for converting a string into the barcode string.  I am trying to convert it to C#.  I have beenb working on it for several days now and cannot get it to work.  I am greatful for any and all help.

Here is the vb script
banner = "Code 128 message conversion"
msgtext = "Enter message to be converted."
 
dim chars
dim chrs
 
valid = false
message = ""
prev = ""
l = 1
while l > 0
    message = getmsg
    if message > "" then
        convmess = convert
        message = inputbox("Copy the text below, and paste it into your" _
            & " application." & vbNewLine & vbNewLine _
            & "You may then enter a new message for another conversion.", _
            banner, convmess)
        valid = false
        l = len(message)
    end if
wend
 
function getmsg
    while not valid
        if message = "" then
            message = inputbox(msgtext, banner, prev)
        end if
        chars = mysplit(message)
        l = len(message)
        if l > 0 then
            valid = true
            for i = 1 to l
                if asc(chars(i)) > 127 then
                    valid = false
                    char = chars(i)
                    exit for
                end if
            next
            if not valid then
                MsgBox "Invalid character: " & char, vbCritical, "Error"
                prev = message
                message = ""
            end if
        else
            valid = true
        end if
    wend
    getmsg = message
end function
 
function convert
    redim chrs(l+1)
    temp = chars
    start = 104
    mode = 0
    m = 1
    for i = 1 to l + 1
        sw = true
        if i < l + 1 then
            digit = asc(temp(i)) > 47 and asc(temp(i)) < 58
            if (mode = 0 and not digit) or (mode = 1 and digit) then sw = false
        end if
        if mode = 0 then
            if sw then
                ind = i
                mode = 1
            else
                chrs(m) = asc(temp(i)) - 32
                m = m + 1
            end if
        elseif sw then
            if i - ind < 4 then
                for j = 0 to i - ind - 1
                    chrs(m+j) = asc(temp(ind+j)) - 32
                next
                m = m + i - ind
                if i < l + 1 then
                    chrs(m) = asc(temp(i)) - 32
                    m = m + 1
                end if
            else
                if ind = 1 then
                    start = 105
                else
                    chrs(m) = 99
                    m = m + 1
                end if
                n = int((i - ind)/2)
                for j = 0 to n - 1
                    chrs(m+j) = 10*temp(ind+2*j) + temp(ind+2*j+1)
                next
                m = m + n
                if i < l + 1 then
                    chrs(m) = 100
                    m = m + 1
                end if
                if (i - ind) mod 2 = 1 then
                    chrs(m) = asc(temp(i-1)) - 32
                    m = m + 1
                end if
                if i < l + 1 then
                    chrs(m) = asc(temp(i)) - 32
                    m = m + 1
                end if
            end if
            mode = 0
        end if
    next
    check = start
    for i = 1 to m - 1
        check = check + chrs(i)*i
    next
    ch = chr((check mod 103) + 32)
    chrs(m) = check mod 103
    for i = 1 to m
        if chrs(i) < 96 then
            chrs(i) = chr(chrs(i) + 32)
        else
            chrs(i) = chr(chrs(i) + 97)
        end if
    next
    convert = chr(start+97) & join(chrs, "") & chr(203)
end function
 
function mysplit(text)
    dim myarray()
    redim myarray(len(text))
    for i = 1 to len(text)
        myarray(i) = mid(text, i, 1)
    next
    mysplit = myarray
end function

Open in new window

Avatar of tomcahill
tomcahill
Flag of Canada image

ASKER

here is the code I have in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Code128
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        static string Convert(string myMessage)
        {
            int length = myMessage.Length + 1;
            string[] chrs = new string[length];
            string[] temp = mySplit(myMessage);
            int start = 104;
            int mode = 0;
            int m = 1;
            bool sw = true;
            bool digit;
            int ind = 0;
            int n = 0;
            int check;
 
            for (int i = 1; i == length; i++)
            {
                sw = true;
                if (i < length)
                {
                    digit = Asc(temp[i]) > 47 && Asc(temp[i]) < 58;
                    if ((mode == 0 && !digit) || (mode == 1 && digit))
                    {
                        sw=false;
                    }
                }
                if (mode == 0)
                {
                    if (sw)
                    {
                        ind = 1;
                        mode = 1;
                    }
                    else
                    {
                        chrs[m] = Asc(temp[i]) - 32;
                        m++;
                    }
                }
                else if (sw)
                {
                    if (i - ind < 4)
                    {
                        for (int j=0; j == (i - ind - 1); j++)
                        {
                            chrs[m+j] = Asc(temp[ind + j]) - 32;
                        }
                        m = m + i - ind;
                        if (i < length)
                        {
                            chrs[m] = Asc(temp[i]) - 32;
                        }
                        else
                        {
                            if (ind == 1)
                            {
                                start = 1 - 5;
                            }
                            else
                            {
                                chrs[m] = 99;
                                m++;
                            }
                            n = ((i =ind) /2);
                            for (int j = 0; j == (n - 1); j++)
                            {
                                chrs[m + j] = 10 * temp[ind + 2 * j] + temp[ind + 2 * j + 1];
                            }
                            m = m + n;
                            if (i < length)
                            {
                                chrs[m] = 100;
                                m++;
                            }
                            if (((i - ind) % 2) == 1)
                            {
                                chrs[m] = Asc[temp(i - 1)] - 32;
                                m++;
                            }
                            if (i < length)
                            {
                                chrs[m] = Asc(temp[i] - 32);
                                m++;
                            }
                        }
                        mode = 0;
                    }
                }
 
                
            }
            check = start;
            for (int i = 1; i == (m - 1); i++)
            {
                check = check + chrs[i] * 1;
            }
            char ch = Chr((check % 103) + 32);
            chrs[m] = check % 103;
            for (int i = 1; i == m; i++)
            {
                if (chrs[i] < 96)
                {
                    chrs[i] = Chr(chrs[i] + 32);
                }
                else
                {
                    chrs[i] = Chr(chrs[i] + 97);
                }
            }
            return Chr(start + 97) & string.Join(chrs, "") & Chr(203); 
            
        }
 
        static string[] mySplit(string myMessage)
        {
            int length = myMessage.Length;
            string[] myArray = new string[length];
 
 
            for (int i = 1; i == length; i++)
            {
                myArray[i] = Mid(myMessage, i, 1);
 
            }
 
            return myArray;
 
        }
        public static string Mid(string param, int startIndex, int length)
        {
            //start at the specified index in the string ang get N number of
            //characters depending on the lenght and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }
        public static byte Asc(char src)
        {
            return (System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(src + "")[0]);
        }
 
        public static char Chr(byte src)
        {
            return (System.Text.Encoding.GetEncoding("iso-8859-1").GetChars(new byte[] { src })[0]);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(mySplit(textBox1.Text.Trim()).Length.ToString());
        }
 
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of idautomation
idautomation
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had already found that and am using it.  Thank you for responding though!