In many Arduino projects, we often extract data from some devices like raspberry pi, or a web database for some arithmetic commutation, but the data is in the form of strings. There is a function in Arduino through which we can convert the strings into integers so that we can perform arithmetic operations on them.
In this write-up, we will conversion of the string to integers in Arduino by using a built-in function toInt().
How to convert the string to Integer
In Arduino, there are many built-in functions that make it easy for us to accomplish different tasks, one of them is the toInt() function. The toInt() function is used for the conversion of strings to integers. To understand it more briefly, we will consider the following code:
int num;
void setup(){
Serial.begin(9600);
num=a.toInt();
Serial.print("The converted string into integer is: ");
Serial.print(num);
}
void loop(){
}
In the above code, we have declared the two variables; “a” with the string data type and “num” with the integer data type. Then we converted the string to an integer by using the toInt() function and stored the result in “num” variable. Finally, display the result on the serial monitor at a baud rate of 9600.
The output of the above code is:
We have seen that the string is converted to an integer and then stored in a num variable. Consider another example in which we try to store the string directly into an integer variable using the code:
void setup(){
Serial.begin(9600);
num=a;
Serial.print("The converted string into integer is: ");
Serial.print(num);
}
void loop(){
}
The above code is the same as we executed above using the toInt() function, but the difference in both of them is; in this code, we tried to save the string directly into the integer value. The output is:
It generated the error of “error: cannot convert ‘String’ to ‘int’ in assignment”, which means we can store the string directly into the integer variable.
Restriction of using the toInt() function
There is a restriction of using the toInt() function; that is; it should not start with any character else it will give a zero value. For example, we run the first code of using toInt() and store the value “a1200” instead of the “1200”:
void setup(){
Serial.begin(9600);
num=a.toInt();
Serial.print("The converted string into integer is: ");
Serial.print(num);
}
void loop(){
}
The output is as:
Similarly, if you pass a string of characters through the toInt() function, the result will be zero. Consider the code again with a=”LinuxHint”:
void setup(){
Serial.begin(9600);
num=a.toInt();
Serial.print("The converted string into integer is: ");
Serial.print(num);
}
void loop(){
}
The output will be:
Conclusion
The built-in toInt() function is used for the conversion of the string to int values. This conversion is needed when we are extracting the data from some other device and we need the data in the int data type instead of strings. In this write-up, we have discussed the built-in function for the conversion of string data type to int data type in Arduino with the help of examples.