. Advertisement .
..3..
. Advertisement .
..4..
This tutorial will show you multiple ways to carry out an Arduino char to int. It shares many similarities with C++, the language this platform is based on.
Arduino Char To Int Conversion
With Implicit Type Casting
Type casting, or type conversion, refers to how one data type is converted to another data type. When you use an Arduino program and declare some constants and variables, the expressions you use may result in a different data type, for example. To sustain this, we need different ways to convert types in C.
There are two forms of type casting in this programming language: explicit type casting and implicit type casting. If you aren’t familiar with C or C++, these names may sound strange. But actually, other languages have one or both of them too.
Explicit type casting is done by the programmer when they write the code of the program. Typically, they alter the data type by using a function or a type cast operator. In other languages, this is usually called type conversion.
On the other hand, implicit type casting occurs when the compiler notices a difference in type within an expression. To prevent undefined behaviors like data loss, the compiler automatically converts one data type to another in order to evaluate the expression successfully. The developer doesn’t need to specify any type casting operator. That is why it is also called automatic type conversion.
In Arduino, a common way to do so is to promote the rank of the right side to that of the left side. The program will evaluate the right side after the promotion and assign it to the left side. This kind of type casting is known as standard conversion. The Arduino compiler can apply it to all fundamental data types, including char and int.
The following example shows how you can convert char to int in Arduino with implicit type casting:
void loop() {
char myChar = '8';
int myInt = myChar - '2'; // myInt = 6
}
In the second expression, the left side is an int variable, while the right side has one char variable and one char liberal. The Arduino compiler will automatically convert myChar and ‘2’ to two int values (8 and 2) and evaluate this operation, which results in 6. You don’t need to use any function or type casting operator to specify this conversion.
With String.toInt()
toInt()
is a method of the String class. It can convert a string to an integer. If your input is a string with more than one character, make sure it begins with an integer number.
Since we have a char value, we also need to convert it to a string first with the string constructor. Its syntax:
String(val)
In which val is the variable or constant you want to convert to a string. This constructor allows many data types, such as char or byte, and returns an instance of the String class.
This example combines these functions to rewrite the previous conversion:
void loop(){
char myChar = '8';
String myStr = String(myChar)
int myInt = myStr.toInt(); // myInt = 8
}
Since we specify where the conversion happens and how it should be done, this is an explicit conversion.
Summary
You can use the String.toInt()
method or implicit type casting to carry out an Arduino char to int conversion. The first solution offers more control over your conversion, but the latter automatically does it and doesn’t require you to specify any casting operator or function.
Note: if you want to do the opposition conversion, this guide will show you how to convert int to char array.
Leave a comment