[comp.lang.c] "1234" ==> 1234

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

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

nfs@hart.Princeton.EDU (Norbert Schlenker) (12/15/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.

Here's a theory of conversion a couple of us thought up while playing
bridge the other night.  Take the outputs from your system standard
random number generator, convert them to a string, and compare them to
the input string.  If the strings don't match, try another random number.
If your random number generator is full period and the input string is
in range and in canonical form, this approach is guaranteed to succeed.

Following is some code to implement this incredible new theory.  We tested
the code successfully on a new 24 MIPS DEC machine that is just undergoing
shakedown testing here.  Conversion of "1234" to integer form took a mere
907 minutes of CPU time.

We are currently examining a few alternative theories.  Promising candidates
include hashing techniques, divide and conquer methods, and a still nebulous
proposal to map strings into flow networks which can then be solved by
your very own proprietary network flow code.  The more systems oriented
people here have suggested a combination of the above techniques, perhaps
with all running in parallel via forked processes and with communication
over pipes.

Adam Buchsbaum
Norbert Schlenker

-------------------------------------------------------------------------
/* An implementation of an alternative theory for converting C strings
 * to the corresponding integer.  Note that this routine requires the
 * supplied rand() function to exhaust the entire space of integers
 * at some point.  Beware of systems which cannot guarantee this.
 * Note also that the input string had better be in canonical form:
 * left justified, no leading zeros, no sign, no extraneous trash.
 *
 * Interface:
 *   int dweeb(char *string);
 */

#define MAXINT_LEN 10		/* maximum digits in an integer */

void srand();
int rand();

char *itoa(n)
int n;
{
  static char s[MAXINT_LEN + 1];
  char *p;

  p = &s[MAXINT_LEN];
  *p = '\0';
  while (n != 0) {
    *--p = '0' + n % 10;
    n /= 10;
  }
  return p;
}

int dweeb(p)
char *p;
{
  int candidate;		/* potential integer equivalent of parameter */
  char *candidate_string = "";	/* pointer to string equivalent of candidate */

  srand(1);
  while (strcmp(p, candidate_string) != 0) {
    candidate = rand();
    candidate_string = itoa(candidate);
  }
  return candidate;
}

userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) (12/17/90)

In article <5372@rossignol.Princeton.EDU>, nfs@hart.Princeton.EDU (Norbert Schlenker) writes:
>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.
>
>Here's a theory of conversion a couple of us thought up while playing
>bridge the other night.  Take the outputs from your system standard
 
<<<many highly useful examples deleted to save space>>>
 
Come on, Norbert, Bryon asked us to "please send whatever you
know or think". I am quite sure you are holding back something.
Surely you know and think somewhat more than you volunteered.
 
-------------------+-------------------------------------------
Al Dunbar          |
Edmonton, Alberta  |  "this mind left intentionally blank"
CANADA             |          - Manuel Writer
-------------------+-------------------------------------------

hagins@gamecock.rtp.dg.com (Jody Hagins) (12/18/90)

In article <5372@rossignol.Princeton.EDU>, nfs@hart.Princeton.EDU (Norbert Schlenker) writes:
|> 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.
|> 
|> Here's a theory of conversion a couple of us thought up while playing
|> bridge the other night.  Take the outputs from your system standard
|> random number generator, convert them to a string, and compare them to
|> the input string.  If the strings don't match, try another random number.
|> If your random number generator is full period and the input string is
|> in range and in canonical form, this approach is guaranteed to succeed.


[ C source deleted ]



One of the best responses of the decade.
Couldn't stop laughing.
Now I have to go to the bathroom...


-- 

Jody Hagins             
hagins@gamecock.rtp.dg.com    
Data General Corp.      
62 Alexander Dr.        
RTP, N.C.  27709        
(919) 248-6035          

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).