How to Convert char to int in C++

Updated on July 23, 2024

Article Outline

In this article, we will learn “How to convert char into int  C++,” which is the basic operation in C++. It can be done using various methods, like type casting, standard library functions, or mathematical operations. We will explore different ways to convert char to int.

Using TypeCasting

Typecasting is a feature in the C++ language. It helps to change one type to another type. In this type of casting, we will use the explicit type conversion.

 

Syntax

(type) expression

Method 1:  In this method, we will use explicit type conversion for converting the character into its corresponding ASCII value(integer).

 

Program

#include <iostream> using namespace std;  int main() { char ch = 'B'; cout << int(ch); return 0; }

Output

66

The time complexity is O(1) and The auxiliary space is also O(1)

 

Method 2:  If you want to convert a char to an integer value with the same value rather than ASCII. The following program will help you to do this.

 

The following program demonstrates this code.

 

Program

#include <iostream> using namespace std;  int main() { char a = '10'; int b = a - '0'; cout << b << endl; return 0; }

Output

10
*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Using static_cast

The static_cast function is another function in the C++ programming language. It converts variable data types. The program below demonstrates the static_cast function example.

 

Program

// C++ program to convert char // to int (ASCII Value) using static_cast #include <iostream> using namespace std;  int main() { char ch = 'B'; int N = static_cast<int>(ch); cout << “The Integer value of B” <<N; return 0; }

Output

66

Using sscanf()

The sscanf() function reads the data from the buffer and stores the value in the respective variable. This function is defined in the <cstdio> header file. We can also use this function to convert characters into integers.

 

Syntax of sscanf

int sscanf( const char* buffer, const char* format, ... );

The following program demonstrates the character-to-integer conversion.

 

Program

#include <iostream> using namespace std;  int main() { const char *strInteger = "1234"; int z; sscanf(strInteger, "%d", &z); cout << "nThe integer value of x : " << z; return 0; }

Output

The integer value of z: 1234

Using stoi

This stoi() function converts a string to an integer value. The following C++ program demonstrates how to convert char to int using the stoi() function.

 

Program

// C++ program to convert char // to int using stoi() #include <iostream> #include <string> using namespace std;  int main() { char s1[] = "80"; int x = stoi(s1); cout << "The integer value of x : " << x; return 0; }

Output

The integer value of x: 80

Using atoi()

The atoi() function returns an integer value. If this function successfully executes otherwise, this function will be returned 0. The following program demonstrates the C++ program to convert a char to an integer using teh atoi() function.

 

Program

// C++ program to convert char // to int using atoi() #include <iostream> using namespace std;  // Driver code int main() { const char *str = "334343043";  int y = atoi(str); cout << "nThe integer value of y :" << y; return 0; }

Output

The integer value of y:334343043

Using stringstream

A string stream allows you to associate a string object with a stream, allowing you to read from the string. If you want to use stringstream, we need to include the sstream header file. This class helps us to process input. The following program demonstrates the C++ program to convert char to int using stream.

 

Program

// C++ program to convert char // to int using string stream #include <iostream> #include <string> #include <sstream> using namespace std;  int main() { stringstream string; string << "35334"; int n; string >> n; cout << "The Integer value is: " << n; return 0; }

Output

The Integer value is 35334

Example with Full code in C++

The following program demonstrates the full Conversion code in char to integer in C++ language.

 

Program

#include <iostream> using namespace std; int main() { char ch = '3'; // Convert the character to an integer. int num = ch - '0'; cout << num <<endl; return 0; }

Output

3

Converting char Value to int Adding 0

We can convert char to int by adding the 0. First, we initially typecast the character to an integer using the int(), and later, we will add 0 to the typecased value.

 

Program

#include <iostream> using namespace std; int main() {  char ch = 'd'; int val = (int(ch) + 0); cout << "Integer output of ch "<< val; // Convert the character to an integer.  }

Output

Integer output of ch 100

stoi() Function vs atoi() Function

The following table differentiates stoi() functions and atoi() functions.

 

stoi() function atoi() function
This function can be used by both language c and C++ This function is only used by the C language
This function header file is <string> This function header file is <stdlib.h>
If you use a non-integer value with this function. Then, this function will be given an error. If you use a non-integer value with this function as a starting character, then it gives no errors.

Conclusion

In this article, we learned how to convert characters to integers in C++. C++ is an object-oriented programming language that has many built-in functions in this programming language for doing this. We also saw the explicit typecasting in this article, which converts data type from one type into another type manually.

 

FAQs
The char is a data type in the C++ language. It stores a single character. For example, ‘char value =’ d’.
Yes, A character can be a single letter, number, symbol, or whitespace. For example, char number = ‘1’.
Yes, we can convert char to int directly using arithmetic or standard library functions. Remember, Direct conversion treats the character as its ASCII value.
It is purely based on your specific requirements. If you are dealing with digits, you must prefer simplicity. Character arithmetic is suitable for this type of conversion. If you need error handling and support for more complex conversions, standard library functions or streams may be preferable.

Updated on July 23, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

IIT Courses

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved