hi,
I have a string variable variable containing some numbers. I want to convert it to int.
First thought I had was the Int() function which works just fine if I type the numbers within the (), but when I put a string variable inside:
The is no Int function. There is an int() "function" which is really a macro that hides the fact that you are doing a cast.
int val4 = int(val3);
Resolves to
int val4 = (int)val3;
You can see that val3 is not a type that can be cast to an int.
You need to extract the character array from the String object, using the toCharArray() method, and then use atoi() (ascii to integer) function to convert the array of characters to an int.
Can't you use a search engine? atoi() is one of the simplest functions available. It takes one argument - a NULL terminated array of characters, and returns the number that the string represents.
do I need a library?
There is, at some point, the need to quit dancing around the edge of the pool, and jump in. Try using the atoi function. If you get an unresolved external reference message or an unknown identifier message then you need a library that contains that function. If not, then the Arduino core already knows about the function (it does), and you don't need a library.
I did google it. there are lots of posts telling to use atoi() with arduino. not telling how. the atoi() in C++ is the same? can I use c++ reference as a base?
If the arduino core knows the function it should be mentioned on the Reference.
I tried it in arduino but as a noob I keep getting errors I don't understand.
so I come here for help.
I'll try using it like in C++ and post back after.
If the arduino core knows the function it should be mentioned on the Reference.
No, it shouldn't. The Arduino reference page is for stuff that is specific to the Arduino. Have you ever seen a C++ application on a mainframe or PC cann analogWrite?