Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID


We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Decimal to Binary Converter

bloodclawsbloodclaws Posts: 1Member
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


  • DaiMitnickDaiMitnick Posts: 77Member
    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.