Convert char to int in C++ Using Different Methods.

Convert char to int in C++ Using Different Methods.

3 mins read18.8K Views Comment
clickHere
Atul
Atul Harsha
Senior Manager Content
Updated on Sep 18, 2023 10:43 IST

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

2023_01_Feature-Image-Templates-92.jpg

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++

#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;
}
Copy code

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;
}
Copy code

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.

All About OOPs Concepts in C++
All About OOPs Concepts in C++
Have you ever wondered how complex software systems are so efficiently organized and managed? This is where the principles of Object-Oriented Programming (OOP) in C++ come into play. OOP in...read more
A Guide to Sort Function in C++
A Guide to Sort Function in C++
Have you ever wondered how to organize data in C++ efficiently? The sort function in C++ comes to the rescue, offering a powerful and straightforward way to arrange elements in...read more
Introduction to Inheritance in C++
Introduction to Inheritance in C++
In this blog, we will explore the concept of inheritance in C++ and its access. We will also discuss different types of inheritance, such as single inheritance, multiple inheritance, and...read more

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;
}
Copy code

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;
}
Copy code

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;
}
Copy code

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.

About the Author
author-image
Atul Harsha
Senior Manager Content

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