TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JO GERAERTS
from: KURT KUZBA
date: 1998-04-02 05:51:00
subject: convert char to int...

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)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.