Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42220

Binary to Decimal Converter Loop help

$
0
0
This is an attempt at creating a binary to decimal converter, and my first time using the For loop. I was wondering if I could get a few hints on how to remove one of the Counters (Hitc, Hitd). Please avoid giving a complete answer, as this is a project and I would like to learn as much as possible. Thanks!
Code:

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

        'assign variables
        'no need for number bigger than integer data type
        Dim vstart As Integer
        Dim endloc As Integer
        Dim Hitc As Integer
        Dim vConverter As Integer
        Dim Hitd As Integer
        Dim vTotal As Integer
        'user entered input
        Dim UserInput As String

        'shows how long number is
        endloc = txtUserInput.Text.Length
        'different variable to for hitcounter use
        Hitd = txtUserInput.Text.Length
        'used for substring
        Hitc = txtUserInput.Text.Length - 1
        vTotal = 0

        'error message if blank
        If txtUserInput.Text = "" Then
            MessageBox.Show("Please enter a value into the text box!", "error!")

            'tests if value is numeric
        ElseIf IsNumeric(txtUserInput.Text) Then

            'determines amount of times to loop
            For vstart = endloc - 1 To 0 Step -1
                'moves to each variable from each variable
                UserInput = txtUserInput.Text.Substring(Hitc, 1)
                'If substring returns "1", then perform equation
                'else equation is skipped
                If UserInput = "1" Then
                    'converts single digit to decimal form
                    vConverter = CInt(2 ^ (endloc - Hitd))
                    'adds up total as equation moves on
                    vTotal += vConverter
                End If
                'counter for substring to move from right to left
                Hitc -= 1
                'counter for equation 
                Hitd -= 1


            Next
            'displays total
            MessageBox.Show(vTotal.ToString)
        Else
            'displays error if blank
            MessageBox.Show("Please enter a value into the textbox!", "error!")
        End If

    End Sub


Viewing all articles
Browse latest Browse all 42220

Trending Articles