Convert string to integer

ddecker

Member
Joined
Aug 31, 2021
Messages
6
Programming Experience
1-3
Hello,

I know it has been mentioned so many times.

Just how do I very easily convert a string with a decimal number e.g. string data "1" into an integer?

I want a comparison at the end (if (data2 == 1) {...}) to turn on an LED. Many thanks.

best regards
:slight_smile:
 
I already told you once in another thread not to quote your own post just to say "thank you". Every time you quote something, you make the thread longer. It also suggest to people that there is something in there that you're trying to draw their attention to. In both cases, you're wasting people's time. Only quote a post if you want to make it clear that you're referring to something specific in that post and, even then, only quote the relevant part of the post. If you simply want to add more information then just do that. There's no need to quote a post that is irrelevant to that information.
 
As for the question, I find it hard to believe that you couldn't find that information with a web search. I don't want to discourage people from use this site and others like it but it should be for stuff that you can't find an answer to on your own, not stuff that you can't be bothered to find an answer to. You should ALWAYS do what you can for yourself first, which includes searching the web and reading any relevant documentation.

Anyway, there are a number of ways to convert a String to a number, including the following:
VB.NET:
Dim str1 = "1"
Dim str2 = "2"
Dim str3 = "3"
Dim str4 = "4"

'This will work but CInt should only be used cast, not to convert'
Dim num1 = CInt(str1)

'These two are basically equivalent'
Dim num2 = Integer.Parse(str2)
Dim num3 = Convert.ToInt32(str2)

Dim num4 As Integer

'This performs validation and conversion'
If Integer.TryParse(str4, num4) Then
    'Use num4 here'
Else
    'Invalid input'
End If
 
Back
Top