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:
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
00101001 -> foobar!"£$%^&*()
(line breaks added by me)
And here's ConvertNum2Bin(); this converts 3141 to 0000110001000101 (and 12 to 00001100, of course):
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