Results 1 to 11 of 11

Thread: Label : recover the value of each digit

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Label : recover the value of each digit

    Hello friends,

    I need advice to analyze a label that contains a sequence of 4 numbers 0 or 1. (ex: 0101 or 1101 etc.)
    Each number corresponds to a state.
    Is it possible to recover the value of each digit?

    For example, if the first digit is a 0 then label1.text = "0"
    If the second digit is a 1 then label2.text = "1" etc ...

    Thanks for your help

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Label : recover the value of each digit

    This would one way of doing it...

    Code:
      Dim Source As String = "a0100110101"
    
    
            For Each C As Char In Source
                Select Case True
                    Case Asc(C) = 48
                        Debug.Print("An 0")
                    Case Asc(C) = 49
                        Debug.Print("An 1")
                    Case Else
                        Debug.Print("Not a 0 or and 1")
                End Select
            Next
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: Label : recover the value of each digit

    Quote Originally Posted by Goggy View Post
    This would one way of doing it...

    Code:
      Dim Source As String = "a0100110101"
    
    
            For Each C As Char In Source
                Select Case True
                    Case Asc(C) = 48
                        Debug.Print("An 0")
                    Case Asc(C) = 49
                        Debug.Print("An 1")
                    Case Else
                        Debug.Print("Not a 0 or and 1")
                End Select
            Next

    Thank you for your answer, I start VB and I can not understand your code can you give me an explanation?

  4. #4
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Label : recover the value of each digit

    What happens is,

    The source would be your label that contains the value you want to check.

    The for loop takes every character in the value and checks if its a 0 a 1 or something else...

    asc(c) transforms a charactor to its ascii value... (http://www.asciitable.com/)
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: Label : recover the value of each digit

    I do not want to check if the source label contains 0 or 1 because it is a certainty.
    On the other hand I wish to separate the 4 digits to analyze them

  6. #6
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Label : recover the value of each digit

    It does that for you.... it breaks down the source in its characters and checks if the character is a 0, a 1 or something else...
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Label : recover the value of each digit

    First things first, you should not be checking any Label. Labels are not for data storage. Their purpose is to display something to the user, not to store it. Whatever is being displayed in that Label must have been put there from somewhere by your code and it's that original that you should be using. You should be storing your data in a variable somewhere and displaying the contents of that variable in the Label. When you want to use the data, you use the variable, not the Label. If the data ever changes, you change the variable and then display the variable in the Label.

    As for your problem, you should treat the binary String as though it was an array of Char values, i.e. access each Char by index. That way, you can put your four destination Labels in an array and then use a For loop to access the Char and the corresponding Label by index. There's no need to actually test anything because all you're doing is displaying the individual Chars. It's sill to say if the Char is a 1 then display a 1 and if it's a 0 then display a 0 because the end result is you display the Char, so do that.

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: Label : recover the value of each digit

    Ok you're right I'll try to find other solutions

  9. #9
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Label : recover the value of each digit

    This is an other approach;

    Code:
    Dim Source As String = "a0100110101"
    
            For I As Integer = 0 To Source.Length - 1
                If Source(I) = "0" Then
                    ' Do something 
                ElseIf Source(I) = "1" Then
                    ' Do something
                Else
                    ' Do Nothing...
                End If
            Next
    Last edited by Goggy; Dec 19th, 2017 at 11:04 AM.
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Label : recover the value of each digit

    Here are some ideas using an enumeration with a flags attribute and convert for binary string conversion.

    Code:
    Public Class Form1
    
        <Flags> Enum SomeStates As Integer
            'note that each state is represented by one bit
            a = 1 << 0
            b = 1 << 1
            c = 1 << 2
            d = 1 << 3
            ' ... etc.
            last = 1 << 31
        End Enum
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim foo As SomeStates = SomeStates.a Or SomeStates.b Or SomeStates.d Or SomeStates.last
            If foo.HasFlag(SomeStates.b) Then
                Stop
            End If
            Dim s As String
            s = foo.ToString
            Debug.WriteLine(s)
            s = DirectCast(foo, Integer).ToString
            Debug.WriteLine(s)
    
            Dim states As String = "1001"
            foo = DirectCast(Convert.ToInt32(states, 2), SomeStates)
            Debug.WriteLine(foo)
    
            foo = foo Or SomeStates.last
            s = Convert.ToString(foo, 2)
            Debug.WriteLine(s)
        End Sub
    End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: Label : recover the value of each digit

    Thanks again for your answers

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