Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strtol()

5 views
Skip to first unread message

Robert L Krawitz

unread,
Nov 1, 1986, 12:43:04 PM11/1/86
to
What does this do? It doesn't seem to exist on my 4.3 system. I
suspect it's a SysV library fn, so it seems like I'll have to write it
myself. (The reason I'm cross-posting it to net.bicycle is that
that's where the particlar program came from.)

Example:

long strtol();

getnum()
{
return (int)strtol(lineptr, &lineptr, 10);
}
--
Robert^Z

David Langdon

unread,
Nov 3, 1986, 11:54:18 AM11/3/86
to
> Xref: rabbit1 net.unix:1814 net.bicycle:619
> r...@mit-trillian.MIT.EDU (Robert L Krawitz): writes

If I'm not mistaken, strtol() is "string-to-long" conversion routine. It
takes an ASCII string and converts it to a long value type

--
David Langdon Rabbit Software Corp.
(215) 647-0440 7 Great Valley Parkway East Malvern PA 19355

...!ihnp4!{cbmvax,cuuxb}!hutch!dml ...!psuvax1!burdvax!hutch!dml

Guy Harris

unread,
Nov 3, 1986, 2:18:02 PM11/3/86
to
> What does this do? It doesn't seem to exist on my 4.3 system. I
> suspect it's a SysV library fn, so it seems like I'll have to write it
> myself.

Yes, it's a generalization of "atol"; it first appeared in System V. It is
passed a pointer to a "char *"; if this pointer is non-null, it will return
a pointer to the first character after the value it converted. It is also
passed a radix, so it can convert hex or octal numbers; if the radix is 0,
it converts the number as if it were a C constant (if it begins with "0x",
it's hex; otherwise, if it begins with "0", it's octal; otherwise, it's
decimal).

> getnum()
> {
> return (int)strtol(lineptr, &lineptr, 10);
> }

As you can probably infer, the "pointer to pointer" is the second argument
and the radix is the third argument.
--
Guy Harris
{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
g...@sun.com (or g...@sun.arpa)

car...@snail.cs.uiuc.edu

unread,
Nov 3, 1986, 4:32:00 PM11/3/86
to

From the SysV "Programmers Reference Manual" :

long strtol(str,ptr,base)
char *str,**ptr;
int base;

Strtol returns as a long integer the value represented by the character string
pointed to by str. The string is scanned up to the first character inconsistent
with the base. Leading whitespace is ignored.

If ptr is not NULL, a pointer to the character terminating the scan is
returned.

If base is positive (and <= 36), it is used as the base for conversion. After
an optional leading minus sign, leading zero's are ignored, and a leading
0X or 0x is ignored if base == 16. If base is zero, then a leading zero
indicates base 8, leading 0x or 0X indicates base 16, otherwise it is base
10.

Robert N. Berlinger

unread,
Nov 4, 1986, 7:53:55 PM11/4/86
to
> What does strtol() do?

This is from the AT&T manual.

SYNOPSIS
long strtol (str, ptr, base)
char *str, **ptr;
int base;
DESCRIPTION


Strtol returns as a long integer the value represented by
the character string pointed to by str. The string is
scanned up to the first character inconsistent with the

base. Leading ``white-space'' characters (as defined by
isspace in ctype(3C)) are ignored.

If the value of ptr is not (char **)NULL, a pointer to the
character terminating the scan is returned in the location
pointed to by ptr. If no integer can be formed, that
location is set to str, and zero is returned.

If base is positive (and not greater than 36), it is used as
the base for conversion. After an optional leading sign,
leading zeros are ignored, and ``0x'' or ``0X'' is ignored
if base is 16.

If base is zero, the string itself determines the base
thusly: After an optional leading sign a leading zero
indicates octal conversion, and a leading ``0x'' or ``0X''
hexadecimal conversion. Otherwise, decimal conversion is
used.

Truncation from long to int can, of course, take place upon
assignment or by an explicit cast.

Hope that helps.
--
Robert N. Berlinger
Systems Analyst
Scientific Computing Center
Albert Einstein College of Medicine

UUCP: ...{philabs,cucard,pegasus,ima,rocky2}!aecom!naftoli
Compuserve: 73047,741
Easylink: 62956067
GEnie: R.Berlinger

Greg Franks

unread,
Nov 6, 1986, 2:34:06 PM11/6/86
to
In article <13...@mit-trillian.MIT.EDU> r...@athena.MIT.EDU writes:
>What does this do? It doesn't seem to exist on my 4.3 system. I
>suspect it's a SysV library fn, so it seems like I'll have to write it
>myself. (The reason I'm cross-posting it to net.bicycle is that
>that's where the particlar program came from.)

Our XENIX systems lack strtol too. My solution:

getnum()
{
#ifdef V7
int i;
if ( sscanf( lineptr, "%d", &i ) > 0 ) {
while( isspace( *lineptr ) )
lineptr++;
while( isdigit( *lineptr ) )
lineptr++;
} else
i = 0;
return( i );
#else
return (int)strtol(lineptr, &lineptr, 10);
#endif
}

Strtol(3) adjusts lineptr by the number of characters eaten. So, after
successfully grabbing the number using sscanf (which skips white space)
adjust the pointer the hard way.

There are public domain versions of strtok. Henry Spencer (utzoo)
wrote one - I used his...
Good luck!

..greg

0 new messages