Problems Converting Binary to Text

tommyready

Member
Joined
Jul 1, 2009
Messages
23
Programming Experience
3-5
Okay here is the code I'm using to convert text to binary and then back to text for display.

VB.NET:
Module convertbinary

    Public Function ConvertToText(ByVal BinText As String)
        Dim BinChar As String
        Dim CharX As String
        Dim ConvertedMessage As String = ""
        Dim Z As Integer
        Dim NewChar As Integer
        Dim Bx As Integer
        Dim BBx As Integer
        For Bx = 1 To Len(BinText)
            BinChar = Mid(BinText, Bx, 8)
            Z = 128
            NewChar = 0
            For BBx = 1 To 8
                CharX = Mid(BinChar, BBx, 1)
                If CharX = "1" Then
                    NewChar = NewChar + Z
                    Z = Z / 2
                Else
                    Z = Z / 2
                End If
            Next BBx
            ConvertedMessage = ConvertedMessage & Chr(NewChar)
            Bx = Bx + 7
        Next Bx
        Return ConvertedMessage
    End Function

    Public Function ConvertBin(ByVal TheString As String)
        Dim C As Integer
        Dim DD As Integer
        Dim EE As Integer
        Dim BinArray(7) As String
        Dim TempString As String = ""
        Dim NewTheString As String = ""
        For C = 1 To Len(TheString)
            DD = Asc(Mid(TheString, C, 1))
            BinArray(7) = DD Mod 2
            DD = DD / 2
            BinArray(6) = DD Mod 2
            DD = DD / 2
            BinArray(5) = DD Mod 2
            DD = DD / 2
            BinArray(4) = DD Mod 2
            DD = DD / 2
            BinArray(3) = DD Mod 2
            DD = DD / 2
            BinArray(2) = DD Mod 2
            DD = DD / 2
            BinArray(1) = DD Mod 2
            DD = DD / 2
            BinArray(0) = DD Mod 2
            For EE = 0 To UBound(BinArray)
                TempString = TempString + BinArray(EE)
            Next EE
            NewTheString = NewTheString + TempString
            TempString = ""
        Next C
        TheString = NewTheString
        Return NewTheString
    End Function
End Module

Here is an example of the problem

ConvertBin("Test") = 01010100101001011001010110010100
And that seems to work fine.

But when I try to reverse it
ConvertToText("01010100101001011001010110010100") = T¥•”

Can anyone look at the code and tell me why this would happen or maybe guide me in the right direction.


What I'm trying to do is store a query string in a table cell. So I figured the best way would be to convert the query string to some binary format for storing and then when I pull it out of the database I would convert it back to a string of text. Any help would be great thanks!
 
You're not converting it to binary anyway. You're simply converting it to a much longer string that contains a binary representation. If you really want to convert between text and binary data then you need to produce a Byte array, not another String. To do that you simply make one method call. You get an Encoding object of the appropriate type and call GetBytes or GetString, depending on which direction your converting.

As for what you're doing now, there's a much simpler way to convert between characters and a binary string:
VB.NET:
Private Function ConvertToPlainText(ByVal binaryString As String) As String
    Dim startIndex As Integer = 0
    Dim plainText As New StringBuilder
    Dim binaryChar As String
    Dim asciiCode As Integer

    While startIndex < binaryString.Length - 1
        binaryChar = binaryString.Substring(startIndex, 8)
        asciiCode = Convert.ToInt32(binaryChar, 2)
        plainText.Append(Convert.ToChar(asciiCode))

        startIndex += 8
    End While

    Return plainText.ToString()
End Function

Private Function ConvertToBinaryString(ByVal plainText As String) As String
    Dim startIndex As Integer = 0
    Dim binaryString As New StringBuilder
    Dim binaryChar As String
    Dim asciiCode As Integer

    For Each ch As Char In plainText
        asciiCode = Convert.ToInt32(ch)
        binaryChar = Convert.ToString(asciiCode, 2)
        binaryString.Append(binaryChar.PadLeft(8, "0"c))
    Next

    Return binaryString.ToString()
End Function
 
Hi,
I saw your answer to this conversion problem and really liked it! I was doing a really long version to get the same result. Your code is tight!
Had to make a couple of adjustments for it to work for me though.

I had to change "As New StringBuilder" to "As New System.Text.StringBuilder"
Also in ConvertToPlainText I changed "startIndex += 1" to "startIndex += 8"

But seriously, I love the tighter code! Thank You
 
I had to change "As New StringBuilder" to "As New System.Text.StringBuilder"
The namespace for every type used has to be specified somewhere. I rarely qualify a type name in-place like that. I almost always import the namespace, which means that every type in that namespace can be used unqualified. You can add an Imports statement to the top of a file to import a namespace for just that file. You can also import a namespace project-wide on the References page of the project properties.
Also in ConvertToPlainText I changed "startIndex += 1" to "startIndex += 8"
That was a mistake on my part so thank you for pointing that out. I've updated the original code.
 
Back
Top