How To Convert Char to Int in C++

Are you looking to know how to convert Char to Int in C++? This article will show you the different ways to convert Char to Int.

In C++, most of the Char and Integer values can be represented by ASCII[1] values. Hence if you know the ASCII values for the number that are currently represented in char then you can easily convert those into integers.

Convert Char to Int in C++ Using ASCII Values

Let us see the below example code of how you can convert the number represented in Char to an integer value.

#include <iostream>

using namespace std;
 
int main()
{
    char example = '9';

    //Typecasting the char to int
    //Note that this convert the ASCII 
    //Value of Char to Int
    int convertedNumber = int(example);

    cout<<convertedNumber<<endl;

    return 0;
}

Output:

9

As you can see in the above code, ‘9’ was represented in char, and then with the use of typecasting the char value that reads it in ASCII form and then converts it to an integer value. Hence if you are aware that the char value can be represented in the ASCII value then only you can use this method and it would work.

Convert Char to Int Using Subtraction

This method you can use if you have a character that cannot be represented in ASCII value for example number 20. Hence to convert the char ’20’ in integer we need to use the subtraction method that will provide the ASCII for each digit and then we can add those to get the actual number.

Let us see in the below example code the usage of subtraction to get the Integer from Char in C++.

#include <iostream>

using namespace std;
 
int main()
{
    char example = '8';

    //Using Subtraction to get the ASCII
    //We are subtracting ASCII value of 0 and
    // ASCII value of example
    int convertedNumber = example - '0';

	//Printing the Converted Number
    cout<<convertedNumber<<endl;

    return 0;
}

Output:

8

As per the above code, example - '0' is equivalent to ((int)example) - ((int)'0'), which means the ASCII values of the characters are subtracted from each other. Since 0 comes directly before 1 in the ASCII table (and so on until 9), the difference between the two gives the number that the character a represents.

How To Convert Char to Int in C++

Convert Char* To Int in C++

If you want to convert char* to int in C++ when char* represents the number. And if the char* variable does not represent number then the below methods will not work.

Let us check in below example code below how you can change the char* to Int in C++.

//CPP Program to Initialize Vector
#include <sstream>
#include <iostream>
using namespace std;
 
int main()
{
    char* numberChar = "12";
    int convertedNum;

    stringstream newStream(numberChar);
    
   //Converting Char* to Int using the Stream
    newStream >> convertedNum;

    cout<<convertedNum;

    return 0;
}

Output:

12

As you can see in the above I was able to convert the char* to int using the string stream library. But this method is not 100% secure and you will receive a warning in C++ that you should not convert string to char*.

But if you still want to convert the char* to int ignoring the warning then you can use the above code to resolve this problem.

Wrap Up

I hope you got the answer related to how to convert char to int in C++. I have discussed two methods above that you can use directly to do the conversion.

Let me know in the comment section if you have found any method that is better than the one discussed above I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Find The Sum Of An Array Of Numbers in JavaScript
  2. Easiest and Best Way To Convert Int to String in C++
  3. How To Convert a Char to String in C++ Quickly
  4. Parse (split) or Tokenize a String in C++ Using Delimiter
  5. Different Ways to Sort a Vector In C++
  6. std::stoi Convert String To Numbers in C++ [3 Ways]
  7. How To Pop Front in Vector C++ [pop_front]

Leave a Comment