Convert char to int in C++ Using Different Methods.
Learn how to convert a character to an integer in C++ using various methods like type casting, standard library functions, or mathematical operations. Explore advantages and disadvantages of each method. Examples and edge cases included. Suitable for beginners and experienced programmers
Converting char to int in C++ is a common task. It can be done using different methods like type casting, standard library functions or mathematical operations. We will explore different ways to convert char to int in C++ with examples. This tutorial will help to convert char to int in C++ for both beginners and experienced programmers.
Read More: What is C++
Table of Content
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Method 1: Using Typecasting for ASCII conversion
#include <iostream>using namespace std;
int main() { // Declare a char variable to store the input char c; cout << "Enter a character: "; cin >> c; // Use type casting operator (int) to convert the char to int int i = (int)c; cout << "The integer value of character " << c << " is " << i << endl; return 0;}
Output:
Enter a character: a The integer value of character a is 97 Enter a character: 5 The integer value of character 5 is 53 Enter a character: 55 The integer value of character 5 is 53
Above program takes a character input from the user, converts it to an integer using the type casting operator (int), and then prints the result.
This program is great for ASCII conversion of single character but it is not suitable for:
- Converting a char number to Integer gives unexpected result.
- Converting a string of character to Integer.
Method 2: Adding or Subtracting 0 for ASCII conversion
#include <iostream>using namespace std;
int main() { char c; cout << "Enter a character: "; cin >> c; int i = c + 0; // you can even use 'c - 0' it will give same result cout << "The integer value of character " << c << " is " << i << endl; return 0;}
Output:
Enter a character: a The integer value of character a is 97 Enter a character: 5 The integer value of character 5 is 53 Enter a character: 55 The integer value of character 5 is 53
Above program results in same output. This method is quite similar to the previous method but without using explicit type casting.
NOTE: This code is not suitable for digit conversion β lead to unexpected result. You can make a slight change in the above code for char digit conversion to number: int i = c + β0β. Just adding 0 within the quotes as a char will give the desired result.
Method 3: Using string to interger β stoi function
#include <iostream>#include <string>using namespace std;
int main() { // Declare a char variable with the value of '5' char c = '5';
// Create a string with a single character c string s(1, c);
// Use the stoi function to convert the string to int int i = stoi(s); cout << "The integer value of character " << c << " is " << i << endl; return 0;}
Output:
The integer value of character 5 is 5
- The function stoi is part of the C++ standard library and it converts a string to an integer.
- It throws an exception of type std::invalid_argument if the string is not a valid number.
Method 4: Using ASCII to integer β atoi function
#include <iostream>using namespace std;
int main() { char c = '5'; int i = atoi(&c); cout << "The integer value of character " << c << " is " << i << endl; return 0;}
Output:
The integer value of character 5 is 5
- The function atoi() is part of the C standard library
- It converts converts a string of characters to an integer.
Method 5: Using sscanf function
#include <iostream>#include <cstdio>using namespace std;
int main() { string s; cout << "Enter a string of characters: "; cin >> s; int i; int result = std::sscanf(s.c_str(), "%d", &i); if (result == 1) cout << "The integer value of the string " << s << " is " << i << endl; else cout << "Invalid input, the string is not a number" << endl; return 0;}
Output:
Enter a string of characters: 444 The integer value of the string 444 is 444
- std::sscanf() function to convert the input string to an integer.
- This function takes two arguments: a pointer to the input string and a format specifier for the integer type.
- The function returns the number of items that were successfully converted and stored.
- In this case, the function returns 1, it means that the conversion was successful and print the appropriate message.
As you saw in this post, there are multiple ways to convert a string of any length of characters to an integer in C++. Each method has its own advantages and disadvantages. Choose the one that best fits your needs.
Conclusion
Thus, converting a character to an integer in C++ is a fundamental task showcasing the languageβs versatility and power. Each method carries its own set of advantages and contexts that are more applicable, encouraging programmers to understand and analyze the problem before choosing the best-suited approach.
Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio