Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a char to int in Arduino

I receive some data in a char variable, and the result in teststring is always a number. How can I convert this number to a variable int?

After that I can put the int variable on delay time. There is a piece of my code:

String readString = String(30);
String teststring = String(100);
int convertedstring;

teststring = readString.substring(14, 18); (Result is 1000)

digitalWrite(start_pin, HIGH);
delay(convertedstring); // Result of teststring convert
digitalWrite(start_pin, LOW);
like image 378
milhas Avatar asked Sep 11 '14 14:09

milhas


People also ask

How do I convert a char to an int?

In Java, we can convert the Char to Int using different approaches. If we direct assign char variable to int, it will return the ASCII value of a given character. If the char variable contains an int value, we can get the int value by calling Character. getNumericValue(char) method.

Can I convert string to int in Arduino?

Allows you to convert a String to an integer number. The toInt() function allows you to convert a String to an integer number. In this example, the board reads a serial input string until it sees a newline, then converts the string to a number if the characters are digits.

How do I convert a char to a string in Arduino?

To convert char to String we can use the String() function. This function takes a variable as an input and returns a String object. In the above code, myChar is a variable of type char to store the given char and myString is a variable of type String to store the conversion result.

What is Atoi in Arduino?

The atoi() function converts a string to an integer value. It may be because I read somewhere that Arduino now has a string library with the software so I do not need to unzip and drag a WString library to the arduino library. Case is important. The Arduino has a String library.


1 Answers

Use:

long number = atol(input); // Notice the function change to atoL

Or, if you want to use only positive values:

Code:

unsigned long number = strtoul(input, NULL, 10);

Reference: http://www.mkssoftware.com/docs/man3/atol.3.asp

Or,

int convertedstring = atoi(teststring.c_str());
like image 156
Bouraoui KACEM Avatar answered Sep 20 '22 14:09

Bouraoui KACEM