What is atoi() Function?
The atoi() function converts string characters into an integer value. This function stops reading further when encountered by a first character that is not a number. In atoi(), you cannot convert exponents or decimal numbers.
Syntax
atoi() function can be defined as:
Parameters
This function only has one parameter, a string (str) which is passed to the function. Main highlight of this string parameter is that the function will not modify the input string; it only returns by changing its type to int as a numerical value.
Return Values
If the atoi() function encounters a valid string value it will return the same string as an int with equal numbers, but in case a non-valid input is there, the function will return 0 as output.
Example Code
{
Serial.begin(115200);
int val_1; /* A New variable is defined*/
char string_1[] = "1234"; /* String initiated*/
val_1 = atoi(string_1); /* Atoi function will convert String into Integer*/
Serial.print("String_1 is equal to "); /* String in printed on Serial Monitor*/
Serial.println(string_1);
Serial.print("String_1 converted to integer = ");
Serial.println(val_1); /* Atoi function output is printed as Integer*/
Serial.print("String_1 Multiplied with 2 (1234*2)= "); /* To verify String is Converted into "int" Multiply it with 2*/
Serial.println(val_1*2);
}
void loop(){
}
In the above code we have described the use of atoi() function in Arduino programming. First in setup function serial communication is initiated by defining the baud rate. Next two variables val_1 and string_1 is initiated with data type int and char respectively.
To convert string into int atoi() function is initialized, using this function we can convert any defined string to int. Once the string is converted, we have printed output on the serial monitor. In the last step we have performed a quick check to verify whether the string is properly converted to int or not. We can do this by multiplying any number with int val_1.
Output
Output terminal will display the string initiated inside the sketch and once the string is converted to int we will verify output by multiplying it with an int.
If a valid input is given to atoi() function in the form of string it will return an integer number equal to passed string. If string has no valid input number, then it will return 0 as output. Strings can generally be converted to numbers by having the following parameters:
- A string created entirely from ASCII digits ‘0123456789’.
- ASCII strings beginning with the character ‘+’
- String containing ASCII digits and begin with the character ‘-‘.
Let’s take an example: if a valid string is not given to atoi() function what will it return.
{
Serial.begin(115200);
char string_2[] = "Linuxhint"; /*String_2 is defined*/
int val_2; /*int variable val_2 is initialized*/
val_2 = atoi(string_2); /*Atoi function defined*/
Serial.print("String_2 is equal to ");
Serial.println(string_2); /*String_2 is printed on serial monitor*/
Serial.print("String_2 converted to integer = ");
Serial.println(val_2); /*Atoi function output is printed*/
}
void loop(){
}
Here in this code, we started by defining baud rate to begin serial communication between Arduino and PC. Next line of code describes two variable one is of int data type and second one describes a string “Linuxhint”. Next using atoi function we will pass this string character to int but this time output will be zero as seen in output section below.
Reason of getting this output is no valid string declaration as discussed earlier the program will only return 0.
Output
Output will display initialized string and as there is no valid input argument the return value by atoi function will be 0.
Conclusion
This quick tutorial will guide you using atoi() function in Arduino programming. Using atoi() we can convert any string into an int. Keeping in mind a valid input is given to a string otherwise it will return 0 as an output for all invalid inputs.