How ToTechnology

How to Convert String into the Int Data Type in Arduino

In numerous Arduino projects, we frequently extricate information from certain gadgets like raspberry pi, or a web data set for some math replacement, yet the information is as strings. There is a capacity in Arduino through which we can change over the strings into numbers with the goal that we can perform math procedure on them.

In this review, we will change of the string to whole numbers in Arduino by utilizing an implicit capacity 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:

String a=“1200”;

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:

String a=“1200”;int num;

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”:

String a=“a1200”;int num;

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”:

String a=“LinuxHint”;int num;

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.

Did you like this article?
Share it on any of the following social media channels below to give us your vote. Your feedback helps us improve.

Other related Technologies ideas you might enjoy

Related Articles

Leave a Reply

Your email address will not be published.