VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



convert number base 10 to base 2

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

Rate 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

Download this snippet    Add to My Saved Code

convert number base 10 to base 2 Comments

No comments have been posted about convert number base 10 to base 2. Why not be the first to post a comment about convert number base 10 to base 2.

Post your comment

Subject:
Message:
0/1000 characters