Gday Folks - been reading your starting

vb.net book and love it - still lots to learn however. I have tried to make a little application that converts between binary, oct, decimal and hex numbers. It works ok but if I type in too many numbers I get an overflow error - and the amount I type in I wouldn't expect this.

Following is my code so far - its not real flash and I haven't created any classes or objects. Any help much appreciated.

Public Sub UpdateNumberFields(ByVal DecValue As Decimal, ByVal Base As Int16)

If Base <> 2 Then Me.txtBinary.Text = ConvertToBaseNumber(DecValue, 2)

If Base <> 8 Then Me.txtOctimal.Text = ConvertToBaseNumber(DecValue, 8)

If Base <> 10 Then Me.txtDecimal.Text = ConvertToBaseNumber(DecValue, 10)

If Base <> 16 Then Me.txtHexadecimal.Text = ConvertToBaseNumber(DecValue, 16)

End Sub

Private Sub txtBinary_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBinary.TextChanged

If Me.txtBinary.ContainsFocus = True And Me.txtBinary.Text <> "" Then

UpdateNumberFields(ConvertToDecimal(Me.txtBinary.T ext, 2), 2)

End If

End Sub

Private Sub txtOctimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOctimal.TextChanged

If Me.txtOctimal.ContainsFocus = True And Me.txtOctimal.Text <> "" Then

UpdateNumberFields(ConvertToDecimal(Me.txtOctimal. Text, 8), 8)

End If

End Sub

Private Sub txtHexadecimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHexadecimal.TextChanged

If Me.txtHexadecimal.ContainsFocus = True And Me.txtHexadecimal.Text <> "" Then

UpdateNumberFields(ConvertToDecimal(Me.txtHexadeci mal.Text, 16), 16)

End If

End Sub

Private Sub txtDecimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDecimal.TextChanged

If Me.txtDecimal.ContainsFocus = True And Me.txtDecimal.Text <> "" Then

UpdateNumberFields(Me.txtDecimal.Text, 10)

End If

End Sub

'Convert a decimal to another base

Public Function ConvertToBaseNumber(ByVal passedValue As Decimal, ByVal base As Int16) As String

Dim Counter, Length As Int16

Dim PosValue, DigitValue As Decimal

Dim Result As String

'Step thru each character

Length = FindFirstDigitPos(passedValue, base)

For Counter = Length To 0 Step -1

'calculate the character position value

PosValue = base ^ Counter

'calculate the value of current digit ignoring position (eg the F in 0xFD8E = 15)

DigitValue = Math.Floor(passedValue / PosValue)

'leave in passedValue remainder to be calculated

passedValue = passedValue - (DigitValue * PosValue)

'if 0-9 just add digit to result string

If DigitValue >= 0 And DigitValue <= 9 Then

Result = Result & DigitValue

Else

'if more than 9 add corresponding alpha to text string (eg 10 = A, 11 = B etc)

Result = Result & Chr(DigitValue + 55)

End If

Next

Return Result

End Function

Public Function FindFirstDigitPos(ByVal foobar As Long, ByVal base As Int16) As Int16

Dim Counter, PosValue As Integer

Do

PosValue = base ^ Counter

Counter = Counter + 1

'Do until the value of the position from the right is more than half

Loop While PosValue < foobar / 2

Return Counter

End Function

'take a 2, 8 or 16 base number and return decimal equivalent...

Public Function ConvertToDecimal(ByVal passedVal As String, ByVal Base As Integer) As Decimal

Dim Counter As Integer

Dim DecValue As Decimal

Dim Ubound As Integer = passedVal.Length

'if binary or octimal...

If Base = 2 Or Base = 8 Then

'step through each character in number string...

For Counter = 1 To Ubound

Dim digit = passedVal.Substring(Ubound - Counter, 1)

If digit <> "0" Then

'check digit is legal for number base (eg not over 1 if binary and not over 7 if oct...

If digit > Base - 1 Then MessageBox.Show("Alert - digit can't be greater than " & Base - 1)

'add to decimal result value of digit calculated against base and position....

DecValue = DecValue + (Base ^ (Counter - 1) * digit)

End If

Next

Return DecValue

'if hexadecimal...

ElseIf Base = 16 Then

'convert to Uppercase...

passedVal = UCase(passedVal)

'step through each character

For Counter = 1 To Ubound

Dim digit As String = passedVal.Substring(Ubound - Counter, 1)

If digit <> "0" Then

'if character a number (0-9)?

If Asc(digit) >= 48 And Asc(digit) <= 57 Then

'add character * Base ^ Position

DecValue = DecValue + (Base ^ (Counter - 1) * Val(digit))

'or is character A-F?

ElseIf Asc(digit) >= 65 And Asc(digit) <= 70 Then

'add calculated value in hex

DecValue = DecValue + (16 ^ (Counter - 1) * (Asc(digit) - 55))

'or if illegal report error

Else

MessageBox.Show("Alert: Illegal Character")

End If

End If

Next

Return DecValue

End If

End Function