by tanakorn wichaiwong (2 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 6th October 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)
convert number base 10 to base 2
If TextBox1.Text = "" Then Exit Sub
Dim i As Integer = TextBox1.Text
Dim idigit As Integer
Dim str As String = String.Empty
Dim i2 As Integer = 2
Dim x As Integer
For x = 0 To 100
If 2 ^ x >= i Then
i2 = x
Exit For
End If
Next
Dim t = i2
While t >= 0
str &= getPosition(t, i)
t -= 1
End While
Dim s As String = Microsoft.VisualBasic.Right(str, i2 + 1)
Dim v As String
If Mid(s, 1, 1) = 0 Then
v = Mid(s, 2)
Else
v = s
End If
TextBox2.Text = v
End Sub
Private Function getPosition(ByVal i As Integer, ByRef ivalue As Integer) As String
Try
If 2 ^ i <= ivalue Then
getPosition = "1"
ivalue -= 2 ^ i
Else
getPosition = "0"
End If
Catch ex As Exception
End Try
End Function