Peace
20
Q:

converting char to int in c++

int x = (int)character - 48;
8
int x  = '9' - 48;
// x now equals 9 as an integer
0
// for example you have such integer
int i = 3;

// and you want to convert it to a char so that
char c = '3';

what you need to do is, by adding i to '0'. The reason why it works is because '0' actually means an integer value of 48. '1'..'9' means 49..57. This is a simple addition to find out corresponding character for an single decimal digit integer:

i.e. char c = '0' + i;

If you know how to convert a single decimal digit int to char, whats left is how you can extract individual digit from a more-than-one-decimal-digit integer

it is simply a simple math by making use of / and %

int i = 123 % 10;  // give u last digit, which is 3
int j = 123 / 10;  // give remove the last digit, which is 12
The logic left is the homework you need to do then.
0
#include <sstream>

using namespace std;

int main()
{
    stringstream str;
    
    str << "1";

    double x;
    str >> x;
}
-2
int x = character - '0'
-1

New to Communities?

Join the community