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...
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
associates probably there is an operator missing. add division operator there and that would solve the problem.
i still cant get this to work if anyone has it working, could you please just post the whole, complete module code thanks
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
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
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.
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"
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