Decimal to Binary Converter

I have had to make a decimal to binary converter as part of a assignment which I have done and it displays the correct conversion but now I have to split the conversion into individual text boxes which works but as soon as the conversion answer is less than 8bit it crashes does anyone know how to ad a zero to the text boxes that don't have a in put heres my code:

Public Class Form1
Dim Answer, SpacePos, Length, IndexOf As Integer
Dim BinLength, bin As String, value As Short

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BinLbl.Click

End Sub

Private Sub ConButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConButton.Click

bin = ""
value = Val(DecInput.Text)

If value = 0 Then Exit Do
If value >= 256 Then Exit Do

If value Mod 2 = 0 Then

bin = "0" & bin

value = value / 2

bin = "1" & bin

value = (value - 1) / 2

End If

BinConLbl.Text = bin
BinLength = bin.Length
Bin1.Text = bin(0)
Bin2.Text = bin(1)
Bin3.Text = bin(2)
Bin4.Text = bin(3)
Bin5.Text = bin(4)
Bin6.Text = bin(5)
Bin7.Text = bin(6)
Bin8.Text = bin(7)

End Sub

Private Sub DecInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DecInput.TextChanged

End Sub
End Class


  • My guess would be this line:

    value = value / 2

    And the one similar to it, value is a short, which is a type of integer, which cannot have decimal places, so if value is an odd number, when divided it will try to create a decimal into the short and will just simply fail. Try changing it to double and see if that helps. HTH, Dai

    Do or do not, there is no try. |
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion