"1234" ==> 1234 (char * ==> int)

H. David Bonnett bonnett at seismo.CSS.GOV
Thu Dec 13 09:38:26 AEST 1990


In article <1990Dec12.215359.5378 at cs.utk.edu>, lape at .cs.utk.edu (Bryon S. Lape) writes:
|> 
|> 	I need to know how to convert a string of numbers into an int.
|> For example, "1234" ==> 1234.  I am interested in all theories, so please
|> send whatever you know or think.  Also, please e-mail the responses.
|> 
|> 
|> Bryon

Well,
	Here is a code fragment that works for me. Ignore the incr/decr g
garbage... I got it to work and left it ;-)

Background:

	SunOS 4.1 with the Sun excuse for a compiler (non Ansi)
	pos is a char *
	num is an int;
	mtops & menu are both char * to strings
	mtops is composed of concatted runs of digits and non digit chars.
	This will extract an arbitrary length number from that string.

        {
           pos=(char*)strstr(mtops,menu);
           pos--;
           while (isdigit((int)*pos)) pos--; /* Got back until not digit */
           pos+=1;
           num=(int)strtol(pos,(char  **)NULL,10);
        }

Hope this helps..

-dave bonnett; Center for Seismic Studies
      bonnett at seismo.css.gov-



More information about the Comp.lang.c mailing list