Binary Conversion In VB

vishal sharma's Avatar author of Binary Conversion In VB
This is an article on Binary Conversion In VB in Visual Basic [VB].
Useful Binary Conversion. This program convert Binary to text and text to binary. This program has one Form1 (Main.frm), textbox (Text1.Text), two button, (cmdConvtoBin and cmdConvtoText) and a module1 (Binary.bas)

- Form Command
Code: VB
Private Sub cmdConvtoBin_Click()
    MainText = Text1.Text
    ConvertBin MainText
    Text1.Text = MainText
End Sub

Private Sub cmdConvtoText_Click()
    MainText = Text1.Text
    ConvertToText MainText
    Text1.Text = MainText
End Sub

Private Sub Form_Load()
    Text1.Text = ""
End Sub

- Modules script
Code: VB
Public MainText As String 'this is the text from the textbox

Public Sub ConvertToText(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
    BinText = ConvertedMessage
End Sub

Public Sub ConvertBin(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
    NewTheString = ""
End Sub
more later...
0
associates's Avatar, Join Date: Jul 2006
Newbie Member
Hi vishal sharma,

I was looking for some codes that would convert binary to text and vice versa and have come to your site. I must say this is what i'm looking for.

However, i'd like to ask you a question. with the convertbin sub you have there, there seems to be some missing info. for example,
Code: VB
For C = 1 To Len(TheString)       
         DD = Asc(Mid(TheString, C, 1))         
         BinArray(7) = DD Mod 2       
              DD = DD 2      <-------------------------- error 
         BinArray(6) = DD Mod 2
Could you please clarify this for me?

Thank you in advance

Last edited by shabbir; 24Jul2006 at 10:31.. Reason: Code formating.
0
harish13's Avatar
Go4Expert Member
thank u for the reply
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
associates probably there is an operator missing. add division operator there and that would solve the problem.
0
nubnub's Avatar, Join Date: Aug 2007
Newbie Member
i still cant get this to work

if anyone has it working, could you please just post the whole, complete module code

thanks
0
jwshepherd's Avatar, Join Date: Aug 2007
Team Leader
replace the module with the following.

module- replacement
Code:
Public MainText As String 'this is the text from the textbox

Public Sub ConvertToText(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
    BinText = ConvertedMessage
End Sub

Public Sub ConvertBin(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
    NewTheString = ""
End Sub
0
nubnub's Avatar, Join Date: Aug 2007
Newbie Member
ok it works, sorta.....i translated 'hello' into binary and it gave me '1010100010100101100101001001010010010001'

which, when translated back, comes to be '¨¥””‘' and on a web based converter, it comes out to be '¨¥””‘'

so i experimented with the web based one, and it turns out in the program, the Binary to TeXt works perfectly, but the Text to Binary has something wrong with it...

anyone know whats up?


thanks & cheers
0
nubnub's Avatar, Join Date: Aug 2007
Newbie Member
anyone...know whats going on?
0
Slyvena's Avatar, Join Date: Dec 2008
Newbie Member
Will this work with numbers or along text i.e Is is just characters? Or numbers too. if not can someone post a code that would do basic integer or real number conversion to and fro binary.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
The operator between DD and 2 should be a backslash, not a slash. Presumably this is dropped by the site logic. Interestingly (in VS2005) 13 rounds to 6, but 3 rounds to 2. Not sure why the rounding is in opposite directions for 6.5 and 1.5.

In VS2005 you can't just have Public Sub ConvertBin(TheString As String) - it insists on inserting a ByVal, which means you can't modify the string. So you have to force ByRef.

Code:
Backslash test \ there should be one between "test" and "there"