Howdy, Stranger!

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

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
[Vb.net] Binary Converter [Source]
  • Xin
    Posts: 3,251
    Public Class BinaryConverter
    Dim DecimalNumberLength As Integer

    Dim CurrentCharacter As Char

    Dim ToConvert As String
    Private Sub cmdTextToBinary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTextToBinary.Click
    Dim Val As String = Nothing
    Dim Result As New System.Text.StringBuilder
    For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(txtIn.Text)
    Result.Append(Convert.ToString(Character, 2).PadLeft(8, \"0\"))
    Result.Append(\" \")
    Next
    Val = Result.ToString.Substring(0, Result.ToString.Length - 1)
    txtOut.Text = Val
    End Sub

    Private Sub cmdBinaryToText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBinaryToText.Click
    Dim Val As String = Nothing
    Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(txtIn.Text, \"[^01]\", \"\")
    Dim ByteArray((Characters.Length / 8) - 1) As Byte
    For Index As Integer = 0 To ByteArray.Length - 1
    ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
    Next
    Val = System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
    txtOut.Text = Val
    End Sub

    Private Sub cmdDecimalToBinary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecimalToBinary.Click
    DecimalNumberLength = Len(txtIn.Text)

    ToConvert = txtIn.Text

    txtOut.Text = \"\"

    For Count = 0 To DecimalNumberLength - 1

    CurrentCharacter = ToConvert.Chars(Count)

    If CurrentCharacter = \"0\" Then

    txtOut.Text &= \"0000 \"

    End If

    'Insert an IF Statement for converting each digit from 1-9 here

    If CurrentCharacter = \"1\" Then

    txtOut.Text &= \"0001 \"

    End If

    If CurrentCharacter = \"2\" Then

    txtOut.Text &= \"0010 \"

    End If

    If CurrentCharacter = \"3\" Then

    txtOut.Text &= \"0011 \"

    End If

    If CurrentCharacter = \"4\" Then

    txtOut.Text &= \"0100 \"

    End If

    If CurrentCharacter = \"5\" Then

    txtOut.Text &= \"0101 \"

    End If

    If CurrentCharacter = \"6\" Then

    txtOut.Text &= \"0110 \"

    End If

    If CurrentCharacter = \"7\" Then

    txtOut.Text &= \"0111 \"

    End If

    If CurrentCharacter = \"8\" Then

    txtOut.Text &= \"1000 \"

    End If

    If CurrentCharacter = \"9\" Then

    txtOut.Text &= \"1001 \"

    End If



    Next Count


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
    End Sub

    End Class
    Xin