Binary Conversion In VB

Discussion in 'Visual Basic [VB]' started by vishal sharma, Aug 16, 2004.

  1. vishal sharma

    vishal sharma New Member

    Joined:
    Jul 23, 2004
    Messages:
    106
    Likes Received:
    6
    Trophy Points:
    0
    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:
    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:
    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...
     
  2. associates

    associates New Member

    Joined:
    Jul 24, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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:
        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 a moderator: Jul 24, 2006
  3. harish13

    harish13 New Member

    Joined:
    Jul 20, 2006
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    thank u for the reply
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    associates probably there is an operator missing. add division operator there and that would solve the problem.
     
  5. nubnub

    nubnub New Member

    Joined:
    Aug 28, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    i still cant get this to work

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

    thanks
     
  6. jwshepherd

    jwshepherd New Member

    Joined:
    Aug 13, 2007
    Messages:
    135
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VP Technology Corporation
    Location:
    Texas
    Home Page:
    http://www.officialhackers.com
    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
    
     
  7. nubnub

    nubnub New Member

    Joined:
    Aug 28, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  8. nubnub

    nubnub New Member

    Joined:
    Aug 28, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    anyone...know whats going on?
     
  9. Slyvena

    Slyvena New Member

    Joined:
    Dec 9, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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"
    
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, that seems to work OK. The only other thing I spotted was that the strings weren't initialised; VB issued warnings about strings being used before they were initialised.

    The code doesn't convert numbers, it converts text. So 12 is converted to 0011000100110010, not to 00001100.

    Fixed code follows. The only change to ConvertToText was the addition of ConvertedMessage = "" after the Dims. This is just the ConvertBin function; everything else is as the OP posted it, except for the additional stuff that VB(VS2005) forces, e.g. you can't just have Sub cmdConvtoBin_Click(), it has to be Sub cmdConvtoBin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvtoBin.Click:
    Code:
        Public Sub ConvertBin(ByRef 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
            TempString = ""
            NewTheString = ""
            For C = 1 To Len(TheString)
                DD = Asc(Mid(TheString, C, 1))
                BinArray(7) = DD Mod 2
                DD = DD \ 2 ' backslash operator
                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
        End Sub
    
    Test: hello -> 0110100001100101011011000110110001101111 -> hello

    foobar!"£$%^&*() ->
    0110011001101111011011110110001001100001
    0111001000100001001000101010001100100100
    0010010101011110001001100010101000101000
    00101001 -> foobar!"£$%^&*()
    (line breaks added by me)


    And here's ConvertNum2Bin(); this converts 3141 to 0000110001000101 (and 12 to 00001100, of course):
    Code:
        Public Sub ConvertNum2Bin(ByRef TheString As String)
            Dim DD As Integer
            Dim EE As Integer
            Dim NewTheString As String
            NewTheString = ""
            DD = TheString
            EE = 256
            While (EE < DD)
                EE = EE * 256
            End While
            While (EE > 0)
                If DD And EE Then
                    NewTheString = NewTheString + "1"
                Else
                    NewTheString = NewTheString + "0"
                End If
                EE = EE / 2
            End While
            TheString = NewTheString
        End Sub
    
     
    Last edited: Dec 9, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice