JG> the value is a number is an I want to convert it to a
JG> integer. How can do that. I tried this:
JG> int x=(int)str;
JG> I also tried this:
JG> int x=(int)*str
JG> But if for example value=10 then x becomes something
JG> else. How can I convert it without changing the value?
int x = atoi(str);
atoi() is an ANSI standard function to convert an ascii
string to an integer value.
If you wanted to see how you might do this yourself:
int Int_from_Ascii(const char *szS)
{
int iResult = 0, iSgn = 1;
if(szS && *szS)
for( ; *szS; szS++)
if(isdigit(*szS))
{
iResult = iResult * 10 + *szS - '0';
}
else
if('-' == *szS)
iSgn = -1;
return iResult * iSgn;
}
It isn't pretty, and it isn't quick, but it should work.
atoi() is much faster and much prettier. :)
> ] When you go, go as Mr. Underhill...[ Gandalf the Gray ].....
---
---------------
* Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)
|