Arduino string to int - find out:
The function you need is atoi() which has the prototype:
int atoi( char *str );
Where:
The name of the function is an acronym for ASCII to int - atoi().
Where ASCII is another acronym standing for American Standard Code for Information Interchange. It defines is a table of characters.
int test( ) {
int v
return atoi("2131");
}
The Arduino string to int code code is a lot simpler than itoa() since the string length is fixed (set by user as the input string) and the output type size is also fixed (integer). In addition the base used is,... you guessed it... fixed at base 10 for decimal.
One complication is that the first character could be a minus sign.
So first store that information and move to the next character.
int sign = 0;
if (str[0]=='-') {
sign = 1;
str++'
}
To convert a string to a number start at the least value
digit and work backwards.
char *e = str + strlen(str)-1; // At end character.
Now build up the output value:
int v = 0;
int radix = 1;
while(e>=str) { // Has pointer moved before string start?
if (!isdigit(*e)) return 0; // In ctype.h
v += (*e - '0') * radix;
e--; // Prev character.
radix *= 10;
}
If the value of the character is not a digit then bail out returning zero
The code(*e - '0') gets the character value and changes it to a
number (0-9). This number us multiplied by the current position radix
value or decimal multiplier that increases x10 each time round the loop.
When pointer e
Now account for the stored sign value.
if (sign) v=-v;
return v;
//****************************************************
// Find more Arduino Projects and Tutorials at TronicsBench.com
//
// Written by John Main. Arduino String to int.
//
// Note: _atoi() functions identically to atoi().
//
// MIT license, all text above must be included in any redistribution
// ****************************************************
int _atoi(char *str) {
int sign = 0;
if (str[0]=='-') {
sign = 1;
str++;
}
char *e = str + strlen(str) - 1; // At end character.
int v = 0;
int radix = 1;
while(e>=str) { // Has pointer moved before string start?
if ( ! isdigit(*e) ) return 0; // In ctype.h - Bail out!!!
v += (*e - '0') * radix;
e--; // Prev character.
radix *= 10;
}
if (sign) v=-v;
return v;
}
void setup(void) {
int val;
Serial.begin(115200);
Serial.println("Arduino string to int...");
char num1[] = "123";
char num2[] = "12345";
val = _atoi(num1);
Serial.print("Num 1 "); Serial.println(num1);
Serial.print(" "); Serial.println(val);
val = _atoi(num2);
Serial.print("Num 2 "); Serial.println(num2);
Serial.print(" "); Serial.println(val);
Serial.print("Convert and add ");
val = _atoi(num1) + _atoi(num2);
Serial.print("val ");
Serial.println(val);
Serial.print("Should match ");
Serial.println(123+12345);
Serial.print("Check bad chars: ");
Serial.println(_atoi("123x") );
char num3[] = "-543";
Serial.print("Check negative: ");
Serial.println(_atoi(num3) );
char num4[]="43";
Serial.print("Check(2) negative: ");
Serial.println( _atoi(num3)+_atoi(num4) );
}
void loop(void) {
}
Arduino string to int...
Num 1 123
123
Num 2 12345
12345
Convert and add val 12468
Should match 12468
Check bad chars: 0
Check negative: -543
Check(2) negative: -500
Arduino string to int with RADIX...
binary 101 op should be 5
5
hex 11 o/p should be 17
17
dec -27 o/p should be -27
-27
hex 3f2 o/p should be 930
930
dec 12345 o/p should be 12345
12345
Bin 1010010110100101 = 0xa5a5 o/p should be -23131
-23131
The built in version - atoi() - works as expected; You can easily obtain an integer directly from a string.
You have learned, from this page, how to code the Arduino string to int function - _atoi() - from first principles.
On this page you have also found out how to code a non standard
function that converts a string of different radix (i.e.different to 10)
into an integer:
atoir()
Using this function you can easily convert other base numbers such as binary or hexadecimal into an integer.
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.