lape@.cs.utk.edu (Bryon S. Lape) (12/13/90)
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
bonnett@seismo.CSS.GOV (H. David Bonnett) (12/13/90)
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-
henry@zoo.toronto.edu (Henry Spencer) (12/13/90)
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... Use the atoi() or atol() function from the C library. -- "The average pointer, statistically, |Henry Spencer at U of Toronto Zoology points somewhere in X." -Hugh Redelmeier| henry@zoo.toronto.edu utzoo!henry
rhl@grendel.Princeton.EDU (Robert Lupton (the Good)) (12/13/90)
This is one of those questions that any experienced C programmer can answer before starting to think about it: atoi()or atol(). It is also an obvious RTFM question, and is discussed in some depth in K&R. Please, in cases like this DON'T post the answer -- email it if you must reply. Some of the posted answers were not really correct, but this doesn't invalidate my point, merely adds another: Please don't post answers if you aren't pretty much of an expert yourself. Robert
yiannis@ccad.uiowa.edu (Yiannis Papelis) (12/14/90)
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. > I tried to mail this but it bounced back so: atoi("1234") will work like it has been pointed out. The only problem with that is that you don't get any error checking other than a return value of 0 in which case you don't know if you parsed zero or there was an error. On the other hand int rcode, num; char *s; /* the string to parse */ rcode = sscanf(s, "%d", &num); will work and give back a reliable error code (rcode==1 -> conversion OK rcode == 0 -> no conversion. -- Yiannis E. Papelis -------- Electrical & Computer Engineering yiannis@eng.uiowa.edu -------- University of Iowa
harkcom@potato.pa.Yokogawa.Co.jp (Alton Harkcom) (12/14/90)
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. If you don't have atoi, you can look at page 97 of K&R (2nd ed. ANSI C). -- -- harkcom@pa.yokogawa.co.jp Yokogawa Electric Corporation, Tokyo, Japan
guy@auspex.auspex.com (Guy Harris) (12/20/90)
>Use the atoi() or atol() function from the C library.
Or, if you have it, "strtol()" (if you have ANSI C, you have it; even if
you don't have ANSI C, you may have it). "strtol()" has the advantage
that it tells you where it stopped scanning the string, so you can check
whether the string was a number or just started out as one (e.g., you
may not want to just convert "1234motorway" into 1234; you may want to
let the user know that it's not a number).