How To Make A Binary Converter VB.Net

SOLVED Discussion in 'Web Design & Coding' started by Comptech, Dec 13, 2011.

  1. Comptech

    Comptech ICT Technican

    Dec 11, 2011
    Likes Received:
    For this project you will need the following;

    2 Labels
    2 Textboxes
    2 Buttons

    Name Label 1 "Convert Binary To Text"
    Name Label 2 "Convert Text To Binary"

    Add Textbox 1 Under Label 1
    Add Textbox 2 Under Label 2

    Name Button 1 "Text To Binary"
    Name Button 2 "Binary To Text"

    Desighn the form how you like

    Code for Binary To Text;

    Dim Val As String = Nothing
    Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(TextBox1.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)
    Val = System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
    TextBox2.Text = Val

    Code For Text To Binary;

    Dim Val As String = Nothing
    Dim Result As New System.Text.StringBuilder
    For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text)
    Result.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
    Result.Append(" ")
    Val = Result.ToString.Substring(0, Result.ToString.Length - 1)
    TextBox2.Text = Val

    Hope this helps someone or you do the project for fun

    Kind Regards


    Comptech, Dec 13, 2011
    Zeus likes this.
    1. Advertisements

  2. Comptech

    AceInfinity Somewhere...

    Aug 13, 2012
    Likes Received:
    I'm not sure what kind of input you have for Binary to Text, I didn't try your Text to Binary, but you've got some implicit conversions going on in your code. Set Option Strict On to see what I mean.

    Here's a quick example of a Text to Binary (String to Base 2) code I put together:
    'Text To Binary
    TextBox1.Text = "test"
    Dim input As String = TextBox1.Text
    TextBox2.Text = String.Concat(New UTF8Encoding().GetBytes(input).Select(Function(b) Convert.ToString(b, 2)))
    Here's an example of Binary to Text (Base 2 to String):
    'Binary To Text
    TextBox1.Text = "1110100110010111100111110100"
    Dim input As String = TextBox1.Text
    Dim DivideUp As String = String.Concat(Enumerable.Range(0, CInt(input.Length / 7)).Select(Function(i) input.Substring(i * 7, 7) & " ").ToArray).Trim()
    TextBox2.Text = String.Concat(DivideUp.Split(" "c).Select(Function(s) Convert.ToChar(Convert.ToInt32(s, 2))))
    AceInfinity, Aug 13, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.