ASCII to Binary & Binary to ASCII
-
Wednesday, October 17, 2007 8:45 PMHey 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.
- 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.
- 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.
- I also want to have a File Save so that users can save the text in the Ascii or the Binary RTB's.
QWERTYtech
All Replies
-
Wednesday, October 17, 2007 9:08 PM
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 11:56 PMAfter 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????
-
Thursday, October 18, 2007 12:16 AM
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 BytebtText = 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 - 1s = s + (btText.GetValue(i).ToString)
Next iBinaryRTB.Text = s
HTH,
Vallari
-
Thursday, October 18, 2007 12:36 AMVallari,
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 1:08 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 IntegerlValue = Asc(CharStr)
i = 0
ReDim BinaryArr(i) While lValue <> 0 ReDim Preserve BinaryArr(i)BinaryArr(i) = lValue
Mod 2lValue = lValue \ 2
i = i + 1
End While If UBound(BinaryArr) >= 0 Then For i = 0 To UBound(BinaryArr)BinaryStr = BinaryArr(i) & BinaryStr
NextBinaryStr =
String.Format("0", 8 - Len(BinaryStr)) & BinaryStr End IfCharToBinary = BinaryStr
End FunctionSame way for BinarytoAscii write a function value = value + (2 ^ (8- position of the bit))
-
Thursday, October 18, 2007 1:34 AMSweet.... Is there anyway you could comment this out so I can understand what is all happening???

