kshadow22

Binary Encoding Conversion Visual Basic 2008

Mar 26th, 2014
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class Form4
  2.  
  3.     Private Sub Form4_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  4.         Form2.Show()
  5.     End Sub
  6.  
  7.     Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.  
  9.     End Sub
  10.  
  11.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  12.         Dim Val As String = Nothing
  13.         Dim Result As New System.Text.StringBuilder
  14.         For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text)
  15.             Result.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
  16.             Result.Append(" ")
  17.         Next
  18.         Val = Result.ToString.Substring(0, Result.ToString.Length - 1)
  19.         TextBox2.Text = Val
  20.     End Sub
  21.  
  22.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  23.         Try
  24.             Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(TextBox2.Text, "[^01]", "")
  25.             Dim ByteArray((Characters.Length / 8) - 1) As Byte
  26.             Dim val As String = Nothing
  27.             For Index As Integer = 0 To ByteArray.Length - 1
  28.                 ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
  29.             Next
  30.             val = System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
  31.             TextBox1.Text = val
  32.  
  33.         Catch
  34.             MsgBox("Malformed Binary- Please Try Again.", MsgBoxStyle.Critical, "Error")
  35.             TextBox1.Text = ""
  36.         End Try
  37.     End Sub
  38.  
  39.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  40.         TextBox1.Clear()
  41.     End Sub
  42.  
  43.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  44.         TextBox2.Clear()
  45.     End Sub
  46.  
  47.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  48.         TextBox1.Clear()
  49.         TextBox2.Clear()
  50.     End Sub
  51.  
  52.     Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
  53.         Try
  54.             Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(TextBox1.Text, "[^01]", "")
  55.             Dim ByteArray((Characters.Length / 8) - 1) As Byte
  56.             Dim val As String = Nothing
  57.             For Index As Integer = 0 To ByteArray.Length - 1
  58.                 ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
  59.             Next
  60.             val = System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
  61.             TextBox2.Text = val
  62.  
  63.         Catch
  64.             MsgBox("Malformed Binary- Please Try Again.", MsgBoxStyle.Critical, "Error")
  65.             TextBox2.Text = ""
  66.         End Try
  67.     End Sub
  68.  
  69.     Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
  70.         Dim Val As String = Nothing
  71.         Dim Result As New System.Text.StringBuilder
  72.         For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox2.Text)
  73.             Result.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
  74.             Result.Append(" ")
  75.         Next
  76.         Val = Result.ToString.Substring(0, Result.ToString.Length - 1)
  77.         TextBox1.Text = Val
  78.     End Sub
  79. End Class
RAW Paste Data