How to Convert Char to Int in Arduino
- 
          
            Use the atoi()Function to Convertchartointin Arduino
- 
          
            Use ASCII Values to Convert chartointin Arduino
- 
          
            Use Type Casting to Convert chartointin Arduino
- 
          
            Use the toInt()Function to Convertchartointin Arduino
- 
          
            Use the sscanf()Function to Convertchartointin Arduino
- 
          
            Use the Serial.parseInt()Function to Convertchartointin Arduino
- Conclusion
 
Converting a char to an int is common in Arduino programming. It allows you to manipulate and perform mathematical operations on character data.
This tutorial will discuss six methods to convert a char into an int in Arduino.
Use the atoi() Function to Convert char to int in Arduino
The atoi() function is a standard C library function that converts a string (character array) containing numerical representation into its integer equivalent.
void setup() {
  Serial.begin(9600);
  char charValue[] = "1234";
  int intValue = atoi(charValue);
  Serial.println(intValue);
}
void loop() {
  // Your code here
}
- void setup() { ... }: This is the- setupfunction, where you initialize your code. We start the serial communication at a baud rate of- 9600.
- char charValue[] = "1234";: This line initializes a character array- charValuewith the value- "1234".
- int intValue = atoi(charValue);: Here, we use the- atoi()function to convert the character array- charValueto an integer- intValue.
- Serial.println(intValue);: This line prints the converted integer value to the serial monitor.
Output:
1234
Use ASCII Values to Convert char to int in Arduino
This method can only convert a single char into an int. You must subtract a zero of type char from the char to convert it into an int.
This method exploits the ASCII values to convert characters to integers.
void setup() {
  Serial.begin(9600);
  char charValue = '5';
  int intValue = charValue - '0';
  Serial.println(intValue);
}
void loop() {
  // Your code here
}
- void setup() { ... }: As before, this is the- setupfunction where we initialize serial communication.
- char charValue = '7';: This line initializes a character variable- charValuewith the value- '7'.
- int intValue = int(charValue - '0');: Here, we use type casting to convert the result of- charValue - '0'to an integer.
- Serial.println(intValue);: This line prints the converted integer value to the serial monitor.
Output:
7
Use the toInt() Function to Convert char to int in Arduino
To use the toInt() function to convert a char to an int in Arduino, you’ll first need to convert the char to a String and then use the toInt() function.
void setup() {
  Serial.begin(9600);
  char charValue = '7';
  String stringValue(charValue);
  int intValue = stringValue.toInt();
  Serial.println(intValue);
}
void loop() {
  // Your code here
}
- void setup() { ... }: This is the- setupfunction where we initialize serial communication.
- char charValue = '7';: This line initializes a character variable- charValuewith the value- '7'.
- String stringValue(charValue);: Here, we create a- Stringobject named- stringValueby passing the- charValueto the constructor. This converts the- charto a- String.
- int intValue = stringValue.toInt();: We then use the- toInt()function to convert the- Stringobject to an integer. The resulting integer value is stored in- intValue.
- Serial.println(intValue);: This line prints the converted integer value to the serial monitor.
Output:
7
Use the sscanf() Function to Convert char to int in Arduino
The sscanf() function reads the buffered data based on the specified format.
Syntax:
sscanf(char *data, char *format, [&var1, &var2, ... ]);
Where:
- *datais the pointer to the array.
- *formatis the sequence of characters specifying how the- datashall be interpreted.
- &var1,...is the additional argument specifying the location to save the extracted data.
We can use the below code to read the char to int.
sscanf(someChar, "%d", &result);
Here, %d specifies that the char shall be read as a signed integer decimal.
The complete Arduino code to convert char to int is below.
void setup() {
  Serial.begin(9600); /*Serial Communication begins*/
  char *someChar = "50";
  int result = 0;
  sscanf(someChar, "%d", &result);
  Serial.println(result);
}
void loop() {}
Parsing will stop if no value has been read or a non-digit is read. If no valid input is read until timeout, then 0 will be returned.
See Serial.setTimeout() to set the timeout of the serial. Check this link for more information about the Serial.parseInt() function.
Conclusion
In this tutorial, we’ve explored six methods to convert a char to an int in Arduino. Each method has its advantages and use cases, so choose the one that best suits your application.
Understanding these methods will give you greater flexibility in handling character data in your Arduino projects.
