Results 1 to 3 of 3

Thread: String 2 Binary (B2S as well)

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    11

    Unhappy

    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

  2. #2
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    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
    Onerrorgoto

    Dont be to optimistic, the light at the end of the tunnel might be a train

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width