quarta-feira, 5 de setembro de 2012

on



Public Class Form1

    Private Function Verifica() As Boolean

        If Me.TextBox1.Text = String.Empty Then
            MsgBox("Informe o texto para que possa ser convertido.")
            Return False
        End If

        Return True
    End Function

    Private Sub btnBinario_Click(sender As System.Object, e As System.EventArgs) Handles btnBinario.Click

        Dim Val As String = Nothing
        Dim Result As New System.Text.StringBuilder

        Try
            If Verifica() = False Then Exit Sub

            For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(Me.TextBox1.Text)
                Result.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
                Result.Append(" ")
            Next

            Val = Result.ToString.Substring(0, Result.ToString.Length - 1)
            Me.TextBox2.Text = Val
            Me.Label2.Text = "Binário"

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub btnTexto_Click(sender As System.Object, e As System.EventArgs) Handles btnTexto.Click

        Dim Val As String = Nothing
        Dim Characteres As String = System.Text.RegularExpressions.Regex.Replace(Me.TextBox1.Text, "[^01]", "")
        Dim Bytearray((Characteres.Length / 8) - 1) As Byte

        Try
            If Verifica() = False Then Exit Sub

            For index As Integer = 0 To Bytearray.Length - 1
                Bytearray(index) = Convert.ToByte(Characteres.Substring(index * 8, 8), 2)
            Next

            Val = System.Text.ASCIIEncoding.ASCII.GetString(Bytearray)
            TextBox2.Text = Val
            Me.Label2.Text = "Texto"

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.TextBox1.Text = Me.TextBox2.Text
        Me.TextBox2.Clear()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.TextBox1.Clear()
        Me.TextBox2.Clear()
        Me.TextBox1.Focus()
    End Sub

End Class

 Espero que gostem...
Abraços até a próxima....

0 comentários:

Postar um comentário