How to convert string variable to integer in Arduino
How to convert a string variable to int, long in Arduino code?
Answer
There are two types of string: String() object and char array.
- If you uses String() object, call myString.toInt()
void setup() {
Serial.begin(9600);
String myString = "125";
int myInt = myString.toInt();
Serial.println(myInt);
}
void loop() {
}
COM6
125
Autoscroll
Clear output
9600 baud
Newline
- If it's a char array, call atoi(myString) .
void setup() {
Serial.begin(9600);
char myString[6] = "126";
int myInt = atoi(myString);
Serial.println(myInt);
}
void loop() {
}
COM6
126
Autoscroll
Clear output
9600 baud
Newline
Buy Arduino
1 × Arduino UNO Buy on Amazon | |
1 × USB 2.0 cable type A/B Buy on Amazon | |
1 × Jumper Wires Buy on Amazon |
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.