-
How do you convert a string to binary for manipulation?
Eg. 0001000 is the string
I want to separate it out into an array format such that
a[0]=0, a[1]=0... a[3]=1 ... a[6] = 0
for manipulation.
After that I wish to get the string again by appending the values together. Can I do something like
string = a[0] + a[1] + a[2]...
andrew
-
one solution
Hello Limmies1
Here is one possible solution for you.
It might not be the most effective but it works
and I hope that it will give you an idea to work with.
On a form put one commandbutton and one label
Code:
Option Explicit
Dim strBin As String
Dim strArr() As String
Dim strTest As String
Dim x As Integer
Private Sub Command1_Click()
'put breakpoint for debug so you see what happens
For x = LBound(strArr) To UBound(strArr)
strTest = strTest & strArr(x)
Next x
Label1.Caption = strTest
End Sub
Private Sub Form_Load()
'put breakpoint and debug so that you see what happens
strBin = "0010011101011" 'give binary value
ReDim strArr(Len(strBin))
For x = 1 To Len(strBin)
strArr(x) = Mid(strBin, x, 1)
Next x
End Sub
-
In the code above, you will end up with one empty element in the array.
Code:
'This line will redim array from 0 to Len of the string
'Unless you specify Option Base 1 in the General Declaration
ReDim strArr(Len(strBin))
Then, in the For loop, you start from 1 to Len of the string, leaving "0" out of the loop, therefore element "0" will never be populated with any value.
Updated code:
Code:
Dim strText As String
Dim arrStr() As String
Dim i As Integer
strText = "0 0100 11101011"
Redim arrStr(1 To Len(strText))
For i = 1 To Len(strText)
arrStr(i) = Mid(strText, i, 1)
Next
Then you can use code provided by OnErrorGoTo to combine array back to the original string.
If you have VB6, then you can use Join function to combine array values back to the original string:
Code:
strText = Join(arrStr, "")
strText now holds the original string from the array