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.
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!
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!