-
Dec 19th, 2017, 03:11 AM
#1
Thread Starter
Member
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
-
Dec 19th, 2017, 03:27 AM
#2
Addicted Member
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
-
Dec 19th, 2017, 04:04 AM
#3
Thread Starter
Member
Re: Label : recover the value of each digit
Originally Posted by Goggy
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?
-
Dec 19th, 2017, 04:08 AM
#4
Addicted Member
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
-
Dec 19th, 2017, 04:13 AM
#5
Thread Starter
Member
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
-
Dec 19th, 2017, 04:23 AM
#6
Addicted Member
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
-
Dec 19th, 2017, 05:38 AM
#7
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.
-
Dec 19th, 2017, 10:26 AM
#8
Thread Starter
Member
Re: Label : recover the value of each digit
Ok you're right I'll try to find other solutions
-
Dec 19th, 2017, 10:31 AM
#9
Addicted Member
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
-
Dec 19th, 2017, 11:27 AM
#10
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
-
Dec 21st, 2017, 01:54 AM
#11
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|