Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Break a sting into variables

Status
Not open for further replies.

TheCandyman

Technical User
Sep 9, 2002
761
0
0
US
If i had a string or even a number like:


0010011


How could i break it a certain length and store it? I know it's possible but i can't seem to find the Keyword.
 
sample

left("0010011",3)
returns "001"

right("0010011",3)
returns "011"

mid("0010011",3,4)
returns "1001"

hope helps
DEK
 
I did the Left, and Mid. but it drops the 00 at the start. I tried it as a Integer and also as a String.


Dim WholeVal As String
Dim val1, val2, val3, val4 As Integer

WholeVal = Str$(txtInputVal.Text)
val1 = Left(WholeVal, 2)
val2 = Mid(WholeVal, 3, 2)
val3 = Mid(WholeVal, 5, 2)
val4 = Mid(WholeVal, 7, 2)

The WholeVal = 10011 It should be 0010011 Is there a way to force it to keep the zeros and not drop them off??
 
STR() assumes it's operating on a number, not a string value - drop the STR and You will should have it working.
 

What about a way to change the binary number into a normal number? Is there a VB keyword for that?
 
You have told the program to use integers not strings:

Code:
 Dim val1, val2, val3, val4 As Integer

Just Dim them as strings

To convert binary strings to Int or Long you need to loop through adding the appropriate power of 2 (make sure you check for MSB=0 first, otherwise number represents a negative)

Use mid function in a loop and loop counter for the power

11001 = 2^5 + 2^4 + 2^1
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Dim val1, val2, val3, val4 As Integer

actually declares three of htese as VARIANT and one as integer.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Code:
Public Function basBin2Lng(BinIn As String) As Long

    'Michael Red    adapted from Tek-Tips at some date prior to 12/9/2002

    Dim Bin2Long As Long
    Dim Idx As Long
    Dim strBinIn As String

    '? basBin2Lng("00000101001100101100100001")
    '1360674

    strBinIn = StrReverse(BinIn)

    Idx = 1
    While Idx <= Len(strBinIn)
        MyBit = Mid(strBinIn, Idx, 1)
        If (MyBit = &quot;1&quot;) Then
            Bin2Long = Bin2Long + 2 ^ (Idx - 1)
        End If
        Idx = Idx + 1
    Wend

    basBin2Lng = Bin2Long

End Function
MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
use:
FORMAT(left(&quot;0010011&quot;,3),&quot;0000&quot;)
DEK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top