Click to See Complete Forum and Search --> : converting char[2] to an int, is it possible ?


PsSheba
February 6th, 2005, 10:59 AM
Hi !
I have a char array that contains digits such as {'0', '1'}. I want to convrt it to an int (1) like in the following code"


int main()
{
char myInts[3] = {'0','2','\0'};
int myChars = myInts;
return 0;
}


surely int myChars = myInts; is an error but is there a function that can make the convertion ?
Thanks !

NoHero
February 6th, 2005, 11:03 AM
Take a look at the atoi (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_atof.2c_.atoi.2c_._atoi64.2c_.atol.asp) function (ansi to integer) it converts an string (that contains legal digits in ansi code) to an integer:


int main()
{
char myInts[3] = {'0','2','\0'};
int myChars = atoi(myInts);
return 0;
}

PsSheba
February 6th, 2005, 12:26 PM
Thanks a lot ! :wave:

Andreas Masur
February 6th, 2005, 04:38 PM
'atoi' has some drawbacks...take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231054) for alternatives..