In C++, char is a data type that represents a single character, such as 'A' or '5'. Sometimes, we might want to change this character to an int. It is a common task when working with digits or when we want to know the ASCII value of a character. It can be done using several methods, such as type casting , mathematical operations, and standard library functions.
There are several methods that can help to convert a char to an int in C++. Some of them are as follows:
- Convert char to int Using Typecasting
- Using ASCII Value of a Character
- Convert a Digit Character to its Integer Value
- Using atoi for Character Arrays
- Using stoi for std::string
- Convert Character Array to Integer using strtol
Now, we will discuss these methods one by one.
1) Convert char to int Using Typecasting
In C++, typecasting is a way to manually tell the compiler to treat a variable of one type as another type. When we typecast a char to an int, we get the ASCII value of the character.
Syntax
Using C-Style
It has the following syntax:
int num = (int)charVar;
Or using C++ style
int num = static_cast<int>(charVar);
C++ Example to convert char to int using Typecasting
Let us take an example to illustrate how to convert char to int using Typecasting in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() { //Main Function
char ch = 'B';
// C-style cast
int ascii1 = (int)ch;
// C++ style cast
int ascii2 = static_cast<int>(ch);
cout << "Using C-style cast: ASCII value of '" << ch << "' is " << ascii1 << endl;
cout << "Using static_cast: ASCII value of '" << ch << "' is " << ascii2 << endl;
return 0;
}
Output:
Using C-style cast: ASCII value of 'B' is 66
Using static_cast: ASCII value of 'B' is 66
Explanation
In this example, we demonstrate how to convert a character to its ASCII integer value using both C-style casting ((int)ch) and modern C++ casting (static_cast<int>(ch)). Both methods return the ASCII value of the character 'B', which is 66.
2) ASCII Value of a Character
In C++, every character has an ASCII value (a number representing the character in the computer). In C++, every character is actually stored as a number based on the ASCII (American Standard Code for Information Interchange) system.
For example:
- 'A' -> 65
- 'B' -> 66
- '0' -> 48
- '9' -> 57
So, when we convert a char to int, we're actually getting its ASCII value.
Syntax
It has the following syntax:
int ascii = ch;
C++ ASCII Value of a Character Example
Let us take an example to illustrate how to convert char to int using ASCII value of a character in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() { //Main Function
char ch = 'A';
int asciiValue = ch;
cout << "The ASCII value of " << ch << " is " << asciiValue << endl;
return 0;
}
Output:
The ASCII value of A is 65
Explanation
In this example, we assigned the character ch to an integer variable asciiValue. C++ automatically converts the char to its ASCII value.
3) Convert a Digit Character to its Integer Value
Characters like '1', '2', '3', etc., are not the same as integers 1, 2, 3. They are stored as characters, and we need to convert them properly to use them as numbers. If the character is a number like '5', and we want to get the actual number 5, not the ASCII value, we can subtract '0' from it.
Syntax
It has the following syntax:
int num = digit - '0';
C++ Example to Convert a Digit Char to Int
Let us take an example to illustrate how to convert a digit char to int in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() { //Main Function
char digitChar = '7';
int number = digitChar - '0';
cout << "The integer value is " << number << endl;
return 0;
}
Output:
The integer value is 7
Explanation
In this example, we convert a digit character ('7') to its corresponding integer value by subtracting the character '0' from it. Since characters are internally stored as ASCII values, this operation gives the actual numeric value.
Why does it work?
In ASCII, '0' is 48, '1' is 49, ..., '9' is 57.
So, '7' - '0' = 55 - 48 = 7.
4) Using atoi for Character Arrays
If our number is stored as a string or character array (like "123"), we can use the atoi function.
Syntax
It has the following syntax:
int num = atoi("123");
C++ Example to convert char to int using atoi function
Let us take an example to illustrate how to convert char to int using atoi function in C++.
Example
#include <iostream>
#include <cstdlib> // for atoi()
using namespace std; //using standard namespace
int main() { //Main Function
const char* numStr = "123";
int number = atoi(numStr);
cout << "Converted number is " << number << endl;
return 0;
}
Output:
Converted number is 123
Explanation
In this example, we use the atoi function from the <cstdlib> header to convert a C-style string ("123") into an integer. After that, the resulting integer 123 is printed to the console.
Note: The atoi function only works with character arrays (C-style strings), not with a single char.
5) Using stoi for std::string
If we are using std::string instead of a character array, use the stoi function.
Syntax
It has the following syntax:
int num = stoi("456");
C++ Example to convert char to int using stoi function
Let us take an example to illustrate how to convert char to int using stoi function in C++.
Example
#include <iostream>
#include <string>
using namespace std; //using standard namespace
int main() { //Main Function
string numStr = "456";
int number = stoi(numStr);
cout << "Converted number is " << number << endl;
return 0;
}
Output:
Converted number is 456
Explanation
In this example, we use the stoi function from the <string> header to convert a std::string ("456") into an integer. After that, the converted integer value 456 is printed to the console.
6) Convert Character Array to Integer using strtol
The strtol stands for string to long. It is a function from the C standard library that converts a C-style string (character array) to a long int. It's more powerful than atoi because:
- It allows us to check for errors.
- We can choose the number system (like decimal, hexadecimal, octal).
- We can handle strings with extra characters.
Syntax
It has the following syntax:
long strtol(const char* str, char** endptr, int base);
- str: It represents to the input C-string.
- endptr: It represents a pointer to the first invalid character (can be nullptr if not needed).
- base: It represents the number system (10 for decimal, 16 for hexadecimal, etc.).
C++ Example to convert char to int using strtol
Let us take an example to illustrate how to convert char to int using strtol function in C++.
Example
#include <iostream>
#include <cstdlib> // for strtol
using namespace std; //using standard namespace
int main() { //Main Function
const char* str = "123abc";
char* end;
long number = strtol(str, &end, 10);
cout << "Converted number: " << number << endl;
cout << "Remaining string: " << end << endl;
return 0;
}
Output:
Converted number: 123
Remaining string: abc
Explanation
In this example, we use the strtol function from <cstdlib> to convert the beginning numeric part of a C-style string "123abc" to a long integer. After that, the end pointer points to the remainder of the string ("abc") that was not converted.
Conclusion
In conclusion, converting a char to an int in C++ is easy once we know the difference between ASCII values and numeric characters. Whether we need the ASCII value, want to extract digits, or need to convert a whole string to an integer, C++ gives us multiple ways to do it.