I have a simple loop which is matching the letters and giving the equivalent into binary. Forexample I have a sequence of letters as abc, then according to the code given below the binary generated will be 000010001000011 and saved in the variable at the end.

What I want to have is the inverted output e.g. binary output converted from 000010001000011 (abc) to 000110001000001 (cba).

Can anyone please help me with this piece of code?

For i = 0 To stringlength - 1


            If c = "0" Then
                convertdata = "00000"
            ElseIf c = "a" Then
                convertdata = "00001"
            ElseIf c = "b" Then
                convertdata = "00010"
            ElseIf c = "c" Then
                convertdata = "00011"
            ElseIf c = "d" Then
                convertdata = "00100"
            ElseIf c = "e" Then
                convertdata = "00101"
            ElseIf c = "f" Then
                convertdata = "00110"
            ElseIf c = "g" Then
                convertdata = "00111"
            ElseIf c = "h" Then
                convertdata = "01000"
            ElseIf c = "i" Then
                convertdata = "01001"
            ElseIf c = "j" Then
                convertdata = "01010"
            ElseIf c = "k" Then
                convertdata = "01011"
            ElseIf c = "l" Then
                convertdata = "01100"
            ElseIf c = "m" Then
                convertdata = "01101"
            ElseIf c = "n" Then
                convertdata = "01110"
            ElseIf c = "o" Then
                convertdata = "01111"
            ElseIf c = "p" Then
                convertdata = "10000"
            ElseIf c = "q" Then
                convertdata = "10001"
            ElseIf c = "r" Then
                convertdata = "10010"
            ElseIf c = "s" Then
                convertdata = "10011"
            ElseIf c = "t" Then
                convertdata = "10100"
            ElseIf c = "u" Then
                convertdata = "10101"
            ElseIf c = "v" Then
                convertdata = "10110"
            ElseIf c = "w" Then
                convertdata = "10111"
            ElseIf c = "x" Then
                convertdata = "11000"
            ElseIf c = "y" Then
                convertdata = "11001"
            ElseIf c = "z" Then
                convertdata = "11010"
            ElseIf c = "1" Then
                convertdata = "11011"
            ElseIf c = "2" Then
                convertdata = "11100"
            ElseIf c = "3" Then
                convertdata = "11101"
            ElseIf c = "4" Then
                convertdata = "11110"
            Else
                convertdata = "11111"
            End If


            converted_data &= convertdata


        Next

Regards,

Bilal A. Khan

What I want to have is the inverted output e.g. binary output converted from 000010001000011 (abc) to 000110001000001 (cba).

For i = 0 To stringlength - 1


            If c = "0" Then
                convertdata = "00000"
.... rest of your code


Should be something like this:

Dim stringlength as Int32 = StringToConvert
Dim c as string
For i = 0 To stringlength - 1
    c = StringToConvert(i)

            If c = "0" Then
                convertdata = "00000"
.... rest of your code


To just the reverse result, just change the "For" statement to:
For i = stringlength - 1 to 0 Step -1

I was bored so I wrote you this Function to show you another approach. I am not saying it is better. It is just a different way of doing it.

Private Function MyBinary(ByVal s As String) As String

   Dim alphaOffset As Int32 = Asc("a") - 1

   Dim bits As Int32
   Dim charbits(0 To 4) As Char ' your bit pattern has 5 positions

   ' A StringBuilder is just a class that manages a character array for you
   ' since we can calculate how many characters there will be,
   ' initialize the StringBuilder to this known length
   Dim binary As New System.Text.StringBuilder(s.Length * 5)

   For i As Int32 = s.Length - 1 To 0 Step -1
      If IsNumeric(s(i)) Then
         bits = Int32.Parse(s(i))
         If bits > 4 Then Throw New ArgumentException("Invalid character: " & bits.ToString)
         If bits > 0 Then bits += 26
      Else
         If s(i) < "a"c OrElse s(i) > "z"c Then Throw New ArgumentException("Invalid character: " & s(i))
         bits = Asc(s(i)) - alphaOffset
      End If

      For j As Int32 = 0 To 4
         If (bits And (1 << (4 - j))) = 0 Then
            binary.Append("0"c)
         Else
            binary.Append("1"c)
         End If

      Next j

   Next i
   Return binary.ToString
End Function
This question has already been answered. Start a new discussion instead.