none
ASCII to Binary & Binary to ASCII

    Question

  • Hey everyone,

         I am writing a small app for giggles.  I am writing an app to allow a user to put in ASCII text and convert it to Binary and vice versa.

          Here is everything I'm trying to learn to hopefully teach me some stuff about VB.NET 2005.
    1. How can I convert text in a RTB to Binary and vice versa.  For a little info, I have two RTB's: AsciiRTB and BinaryRTB.
    2. I want to allow a user to open a .txt file and push text from the .txt file to a RichTextBox.  I want to have 2 open files, i.e. Open -> ASCII File & Open -> Binary, and depending on which one is selected then it will be opened into the corresponding RTB.
    3. I also want to have a File Save so that users can save the text in the Ascii or the Binary RTB's.
    If anyone could help with any piece of this, I would greatly appreciate it.

    QWERTYtech
    Wednesday, October 17, 2007 8:45 PM

Answers

  • Hi,

     

    Answer to your 1st question

     

    Dim strText As String = "This is an original string"
    Dim btText() As Byte
    btText = System.Text.Encoding.UTF8.GetBytes(strText)
    MessageBox.Show("The total number of encoded bytes is: " & btText.Length.ToString())

    You can find more inforamtion on http://blogs.techrepublic.com.com/programming-and-development/?p=415 

    HTH,

    Vallari

    Wednesday, October 17, 2007 9:08 PM
  • Hi,

     

    I don't if there is any function available but you can use following code

    Dim strText As String = "This is an original string"

    Dim btText() As Byte

    btText = System.Text.Encoding.UTF8.GetBytes(strText)

    MessageBox.Show("The total number of encoded bytes is: " & btText.Length.ToString())

    Dim i As Integer

    Dim s As String = ""

    For i = 0 To btText.Length - 1

    s = s + (btText.GetValue(i).ToString)

    Next i

    BinaryRTB.Text = s

     

    HTH,

    Vallari

    Thursday, October 18, 2007 12:16 AM
  • Hmmm.. sorry this code is converting string to the bytes and not binary ... you need binary.

     

    You need to write a function to convert Ascii to binary and binary to ASCII.

    Following function converts a character to binary, You can call this function in a loop and get the whole string.

    Public Function CharToBinary(ByVal CharStr As String) As String

    Dim lValue As Integer

    Dim BinaryArr() As String

    Dim BinaryStr As String

    Dim i As Integer

    lValue = Asc(CharStr)

    i = 0

    ReDim BinaryArr(i)

    While lValue <> 0

    ReDim Preserve BinaryArr(i)

    BinaryArr(i) = lValue Mod 2

    lValue = lValue \ 2

    i = i + 1

    End While

    If UBound(BinaryArr) >= 0 Then

    For i = 0 To UBound(BinaryArr)

    BinaryStr = BinaryArr(i) & BinaryStr

    Next

    BinaryStr = String.Format("0", 8 - Len(BinaryStr)) & BinaryStr

    End If

    CharToBinary = BinaryStr

    End Function

     

     

    Same way for BinarytoAscii write a function value = value + (2 ^ (8- position of the bit))

    Thursday, October 18, 2007 1:08 AM

All replies

  • Hi,

     

    Answer to your 1st question

     

    Dim strText As String = "This is an original string"
    Dim btText() As Byte
    btText = System.Text.Encoding.UTF8.GetBytes(strText)
    MessageBox.Show("The total number of encoded bytes is: " & btText.Length.ToString())

    You can find more inforamtion on http://blogs.techrepublic.com.com/programming-and-development/?p=415 

    HTH,

    Vallari

    Wednesday, October 17, 2007 9:08 PM
  • After using the above code, to do all the work and then show a popup window, how can I make it show the Bytes in a RichTextBox called BinaryRTB????


    Wednesday, October 17, 2007 11:56 PM
  • Hi,

     

    I don't if there is any function available but you can use following code

    Dim strText As String = "This is an original string"

    Dim btText() As Byte

    btText = System.Text.Encoding.UTF8.GetBytes(strText)

    MessageBox.Show("The total number of encoded bytes is: " & btText.Length.ToString())

    Dim i As Integer

    Dim s As String = ""

    For i = 0 To btText.Length - 1

    s = s + (btText.GetValue(i).ToString)

    Next i

    BinaryRTB.Text = s

     

    HTH,

    Vallari

    Thursday, October 18, 2007 12:16 AM
  • Vallari,
        Thank you very much for your help... Do you possibly know how to convert from Binary to ASCII????

        Also,
         I put in 'QWERTYtech' to be converted over and the bytes shows to be '81876982848911610199104'.  Whenever I converted it online it only showed 1's and 0's.

        Did I describe something wrong to make me get this output?

    QWERTYtech
    Thursday, October 18, 2007 12:36 AM
  • Hmmm.. sorry this code is converting string to the bytes and not binary ... you need binary.

     

    You need to write a function to convert Ascii to binary and binary to ASCII.

    Following function converts a character to binary, You can call this function in a loop and get the whole string.

    Public Function CharToBinary(ByVal CharStr As String) As String

    Dim lValue As Integer

    Dim BinaryArr() As String

    Dim BinaryStr As String

    Dim i As Integer

    lValue = Asc(CharStr)

    i = 0

    ReDim BinaryArr(i)

    While lValue <> 0

    ReDim Preserve BinaryArr(i)

    BinaryArr(i) = lValue Mod 2

    lValue = lValue \ 2

    i = i + 1

    End While

    If UBound(BinaryArr) >= 0 Then

    For i = 0 To UBound(BinaryArr)

    BinaryStr = BinaryArr(i) & BinaryStr

    Next

    BinaryStr = String.Format("0", 8 - Len(BinaryStr)) & BinaryStr

    End If

    CharToBinary = BinaryStr

    End Function

     

     

    Same way for BinarytoAscii write a function value = value + (2 ^ (8- position of the bit))

    Thursday, October 18, 2007 1:08 AM
  • Sweet.... Is there anyway you could comment this out so I can understand what is all happening???
    Thursday, October 18, 2007 1:34 AM