Introduction
In many cases, we encounter a situation where input is provided in string or character format which needs to be processed as a string. For example, when we collect information from a web page, we have to parse them in integer data type when required.
In this article, we will be exploring some methods to convert char to int in c++. You can also do a free certification of Basics of C++.
Conversion of Char to Int in C++
Let us learn about different methods to convert char to int in c++.
In further sections, we will also be looking for some methods which convert strings in c++ to integers if those are in the range of the respective data type.
There are six most common methods to convert a character data type to an integer data type in C++:
- Typecasting
- Static_cast
- sscanf()
- stoi()
- atoi()
-
String stream
Let us now understand each of these methods with the help of examples.
Typecasting
Typecasting is a method to convert the data type of a variable into another data type wherever it is applicable. It can be done manually and automatically. We will declare the character to be converted and initialize it. Typecasting it and assigning it to an integer variable will convert the char to int c++.
Following is the implementation of char to int in c++ using Typecasting.
#include <bits/stdc++.h>
using namespace std;
int main(){
char c = 'a';
int val = (int)c; //typecasting
cout<<val;
}
Output:
97
This code will typecast and assign the variable c into integer val. And this value will be the ASCII value of the character set to c.
Also, see Literals in C.Fibonacci Series in C++
Static_cast
We will now look at a method of converting char to int in c++ using static_cast.
Static_cast converts between types using a combination of implicit and user-defined conversions. What it does is take a character that needs to be converted into an integer type as an argument and return its ASCII value.
Following is the implementation of char to int in c++ using the Static_cast.
#include <bits/stdc++.h>
using namespace std;
int main(){
char c = 'a';
int val = static_cast<int>(c);
cout<<val;
}
Output:
97
You can try and compile with the help of online c++ compiler.
Here static_cast will convert the character passed as the argument in the static_cast function to its ASCII value.
sscanf()
Reads data from s and stores it in the locations specified by the additional arguments, just like scanf, but this time from s rather than the standard input (stdin). Its specialty is that it takes input from the buffer, which needs to be a pointer variable.
Following is the implementation of char to int in c++ using the sscanf() function.
#include <bits/stdc++.h>
using namespace std;
int main(){
const char *s = "1411";
int val;
sscanf(s, "%d", &val);
cout << val;
}
Output:
1411
You can practice by yourself with the help of online c++ compiler.
sscanf() works the same as scanf; here, we just passed the variable s instead of the standard input and assigned it to an integer type val. Here *s is in the buffer and its value is assigned to val with its data type being an integer.
stoi()
stoi stands for string to integer.
Parses str interpret its content as an integral number of the specified base, which is returned as an int value. What stio does, basically, is to take the string or character array which needs to be converted to integer type as an argument, and return an integer value of that string.
Following is the implementation of char to int in c++ using the stoi() function.
#include <bits/stdc++.h>
using namespace std;
int main(){
char s[] = "1411";
int val = stoi(s);
cout<<val;
}
Output:
1411
Here stoi function takes the argument char array which is going to be converted into integer type and then converts it into integer type val.
atoi()
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int. What it does is take a sequence of characters as input and return its numerical value in integer format.
Following is the implementation of char to int in c++ using atoi() function.
#include <bits/stdc++.h>
using namespace std;
int main(){
const char *s = "1411";
int val = atoi(s);
cout<<val;
}
Output:
1411
Here atoi() function will parse s into val, an integer type. Also if it encounters any character that is not a digit it will return the number formed with the previous characters.
stringstream
A stringstream is a connection between a string object and a stream that allows you to read from it like a stream (like cin). Stringstream requires the sstream header file to be included. When processing input, the stringstream class comes in handy.
Following is the implementation of char to int in c++ using stringstream.
#include <bits/stdc++.h>
using namespace std;
int main(){
stringstream string;
string << "1411";
int val;
string >> val;
cout<<val;
}
Output:
1411
Here we are taking the input in the form of a string stream, then passing the value of the string into val, which is an integer.