Before moving to our topic, let’s discuss what characters and integers are. “Char” is a data type that is used to store a single character like special characters, alphabets, escape sequences, etc. Whereas the “int” is the integer datatype that contains the whole numbers and its size is 4 bytes. While developing huge programs or a module of multiple lines of code that involve different types, sometimes it becomes important to convert the data types from one to another. When it comes to the conversion of a character datatype to an integer data type in C++, it provides us with multiple ways to convert one datatype to the other.
There are multiple ways to convert “char” to “int” which are as follows:
Method 1: Using Typecasting
Typecasting is the method that is used to convert any datatype from one to another. It is also known as type conversion.
Syntax:
dtype(variable_name)
In the provided syntax, the “dtype” is the datatype in which we want to convert the variable, and the “variable_name” that is passed to the dtype() statement is the variable that is to be converted.
If we have a character variable “a” which holds the value “2” and we want to convert it to the integer value, we simply use “int(a)” to convert it to the integer type.
Example 1:
Let us perform an example to understand the working of typecasting more effectively. In this example, we declare a character variable and convert it to an integer value. Let us first include the iostream header file that is used to enable us to use the input-output operations. Then, we move to the main function where we declare a character variable named “char_var” which is responsible for holding the numeric value which is “2”.
In the next line, we use the “cout” statement to which we pass the “int(char_var) – ‘0’ ”. It is which the int(char_var) changes the type from the character to integer and the value “0” is subtracted from the typecasted value. The question may come to your mind, what is the reason for subtracting the “0” value? Whenever we want to convert a datatype of a numeric value, we have to subtract “0” or “48” from it to get the exact variable. If we don’t subtract “0” or “48”, it returns the ASCII value of the integer as output. In the end, we return the null value.
using namespace std;
int main()
{
char char_var = '2';
cout << int(char_var) -'0';
return 0;
}
As shown in the following figure, the integer value “2” is successfully executed without any error.
Method 2: Using Stoi
The stoi() function is used to convert the string type to the integer value. It is the inbuilt function provided by C++. It is mostly used to parse the integer values out of the string values.
Syntax:
Stoi(string, position)
In the provided syntax, we passed two arguments to the stoi() function – the “string” which is to be converted and the “position”. It is not necessary to add the position. It is used in some cases like having an array of “200” values and we want to convert the datatype of a specified value, the position is used.
Example 2:
In this example, we perform the conversion of the char variable to the integer type using the C++ built-in method which is the stoi() method. Let us proceed to our example:
After including the header files, iostream and the string, the iostream is used to perform the input-output operations where the string allows us to work with the collection of characters. Now, diving into our main function, we declare a char array named “arr[]” to which we pass the numeric value “12” and another integer variable which is “var” to which we pass the stoi() method which means that it is responsible for holding the return values of the stoi() method.
We pass the “arr” as an argument to the stoi() method because in this case, we convert the datatype of “arr” to the integer type. In the next line, we simply display the value that is stored in the variable “var”. Lastly, we return the null value.
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream myvar;
myvar << "5";
int var;
myvar >> var;
cout << "Integer value is: " << var;
return 0;
}
As we can see from the result of our output, the value that we stored in the char array “ arr[]” is successfully returned using the integer variable “var” without getting any error.
Method 3: Using Atoi
The atoi() function is the same as the stoi() function. It is also used to convert the string datatype to the integer datatype.
Syntax:
atoi (const char * str);
In the provided syntax, we passed the “const char*str” to the atoi function where the “const char” is the character datatype and the “str” is the variable that is to be converted to the integer datatype.
Example 3:
Let us now check the working of the atoi() function and how it converts the datatype from string to integer. After including the header files, we move towards our main function where we declare two variables. The first variable is the “const char” which is “myvar”. The second variable is the integer variable which is “var”. Same as the stoi() method, we assign our integer variable, the atoi() method, to which we pass our constant character variable “myvar” which is converted to the integer value. Then, the values are assigned to the integer variable “var”. In the next line of code, we simply use the cout statement to display the resulting value of the variable “var” and return the null value.
As shown in the following figure, the value that we stored in the character variable “myvar” is successfully displayed using the integer type variable without any error.
Conclusion
In this manual, we briefly discussed the conversion of character datatype to the integer datatype using multiple methods. We performed multiple examples to make it easy to understand the concept of the datatype conversion. It is the necessary step in some cases when we have large codes. It also becomes necessary to convert a datatype using a simple line where we can convert the datatype of the variable. We don’t have to declare them again and again. We hope that this article is useful for you to understand the working of these methods.