Article View: comp.std.c
Article #2050Re: "1234" ==> 1234 (char * ==> int)
From: H. David Bonnett
Date: Wed, 12 Dec 1990 22:38
Date: Wed, 12 Dec 1990 22:38
34 lines
1641 bytes
1641 bytes
In article <1990Dec12.215359.5378@cs.utk.edu>, lape@.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@seismo.css.gov-
Technical Details
Message-ID:
<49268@seismo.CSS.GOV>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!ncar!gatech!ukma!seismo!seismo.CSS.GOV!bonnettReferences:
<1990Dec12.215359.5378@cs.utk.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!ncar!gatech!ukma!seismo!seismo.CSS.GOV!bonnett From: bonnett@seismo.CSS.GOV (H. David Bonnett) Newsgroups: comp.std.c,comp.lang.c Subject: Re: "1234" ==> 1234 (char * ==> int) Message-ID: <49268@seismo.CSS.GOV> Date: 12 Dec 90 22:38:26 GMT References: <1990Dec12.215359.5378@cs.utk.edu> Sender: usenet@seismo.CSS.GOV Reply-To: bonnett@seismo.CSS.GOV (H. David Bonnett) Followup-To: comp.std.c Organization: Center for Seismic Studies, Arlington, VA Lines: 34