how can i convert from char to int? I keep getting errors, or the conversion changes the number.
etc.
char cVar = '1';
int nVar = (int)cvar;
now...
int == 6532465
why???
Printable View
how can i convert from char to int? I keep getting errors, or the conversion changes the number.
etc.
char cVar = '1';
int nVar = (int)cvar;
now...
int == 6532465
why???
A char is one byte. This means it can hold a maximum value of 2^8 or 128 if signed, 256 if unsigned.
what you are doing will put the ascii val of cVar into nVar
use atoi() in stdlib.h for converting the numerical value in char into an integer.
Code:char *c="12345";
int i;
i=atoi(c);
cout<<"I is : "<<i;
oh, ok... thanks.
Since you are using char to describe it, I would assume you want '0'..'9' in int form, then a simple thing is:
For a string that represents a number, example above is good.Code:char c = '2';
int ic = (int)(c - '0');