PDA

View Full Version : c++ char to Int



mickey
4th October 2006, 23:58
I I'm trying to to convert var a to int with this, but dosn't work? is the use of strtol correct? thanks


char * pEnd;
int num;
const char* a = "v"; //why with 'v' doesn't compile????
num = strtol (a, &pEnd, 10);
cout << " num " << num << endl; //it print alway 0
}

jacek
5th October 2006, 00:32
const char* a = "v"; //why with 'v' doesn't compile????
Because 'v' is "char" not "char *".


it print alway 0
Did you try to use some valid number (like "1") instead of "v"?

mickey
5th October 2006, 00:37
Because 'v' is "char" not "char *".
Did you try to use some valid number (like "1") instead of "v"?
yes in that way work (it print 1); but I knew any char has a in value(eg 'a' =16; b='17', B=32). I'd like to obtain that number....maybe I'm doing the wrong question......

jacek
5th October 2006, 00:45
any char has a in value(eg 'a' =16; b='17', B=32). I'd like to obtain that number...
In that case you don't need any functions:
char c = 'v';
int v = c;