Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Binary Conversion In VB (http://www.go4expert.com/articles/binary-conversion-vb-t118/)

xpi0t0s 9Dec2008 14:06

Re: Binary Conversion In VB
 
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



All times are GMT +5.5. The time now is 20:28.