by Sameet Natekar (6 Submissions)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 29th January 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Converts Decimal numbers to Binary Digits upto 999999999.
API Declarations
'Do not change their names. Let them be text1 & command1 resp'ly.
'The textbox will be used to accept the decimal nos.
'The answer will be displayed in a message box on the command button click.
Dim bin(30) As Integer
Dim i, j As Integer
Dim temp As Long
temp = Text1.Text
i = 0
While temp <> 0
bin(i) = temp Mod 2
temp = temp \ 2
'Note that the division sign is \ & not /. This ensures that the answer is a whole no.
'For example 5/2 = 2.5 but 5\2=2.
i = i + 1
Wend
dec = ""
For j = i - 1 To j > 0 Step -1
dec = dec & bin(j)
Next
MsgBox (dec)
End Sub
Private Sub Text1_Change()
'This code is to prevent the user from making any invalid entries.
'He can enter only integers. no decimals or negative nos.
'The textbox has a Maxlenth of 9 characters.
n = Len(Text1.Text)
rt = 1
For i = 1 To n
x = Mid(Text1.Text, i, 1)
If Not (x >= 0 And x <= 9) Then
rt = rt * 0
End If
Next
If rt = 0 Or Text1.Text = "" Then
Command1.Enabled = False
Else
Command1.Enabled = True
End If
End Sub