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...
Newbie Member
24Jul2006,08:54   #2
associates's Avatar
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.
Go4Expert Member
25Jul2006,18:22   #3
harish13's Avatar
thank u for the reply
Go4Expert Founder
25Jul2006,22:00   #4
shabbir's Avatar
associates probably there is an operator missing. add division operator there and that would solve the problem.
Newbie Member
28Aug2007,07:20   #5
nubnub's Avatar
i still cant get this to work

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

thanks
Team Leader
28Aug2007,08:57   #6
jwshepherd's Avatar
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
Newbie Member
29Aug2007,06:27   #7
nubnub's Avatar
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
Newbie Member
15Dec2007,07:18   #8
nubnub's Avatar
anyone...know whats going on?
Newbie Member
9Dec2008,06:54   #9
Slyvena's Avatar
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.
Mentor
9Dec2008,13:45   #10
xpi0t0s's Avatar
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"