Amiga.org
Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Jose on September 09, 2005, 07:54:20 PM
-
Hi. I noticed vbcc's stdlib.h doesn't inlcude these. Can someone recomment a good substitution ?
:pint:
-
You should go to http://sun.hasenbraten.de/vbcc/ and open a bug report.
Nevertheless I would use one of these:
long atol (char *s)
{
long i = 0;
sscanf (s,"%ld",&i);
return (i);
}
long atol (char *s)
{
long i = 0;
while (*s >= '0' && *s <= '9')
{
i = (i * 10) + (*s - '0');
s ++;
}
return (i);
}
-
long atol (char *s)
{
long i = 0;
while (*s >= '0' && *s <= '9')
{
i = (i * 10) + (*s - '0');
s ++;
}
return (i);
}
And a working version:
#include
#include
#include
long atol(char *s)
{
int sign = 1;
long i = 0, ni;
while (isblank(*s))
{
s++;
}
if (*s == '+')
{
s++;
}
else if (*s == '-')
{
sign = -1;
s++;
}
while (*s >= '0' && *s <= '9')
{
ni = (i * 10) + (*s - '0');
if (ni < i)
{
errno = ERANGE;
return sign > 0 ? LONG_MAX : LONG_MIN;
}
i = ni;
s++;
}
return sign * i;
}
-
itoa && ltoa
I noticed vbcc's stdlib.h doesn't inlcude these.
...and for a reason, those are not ANSI C functions.
What do they do anyway?
[EDIT]
Ahh, ok, google is your friend (http://dragonsgate.net/pipermail/icc-mot/2004-January/000802.html).
// maximum string buffer needed, plus one for a minus sign when negative
#define MAX_LENGTH 32+1
char* ltoa(char* buf, long i, int base)
{
char reverse[MAX_LENGTH+1]; // plus one for the null
char* s;
char sign = i < 0;
i = labs(i);
reverse[MAX_LENGTH] = 0;
s = reverse+MAX_LENGTH;
do {
*--s = "0123456789abcdefghijklmnopqrstuvwxyz"[i % base];
i /= base;
} while (i);
if (sign) *--s = '-';
return strcpy(buf, s);
}
(From the abovementioned post by John Passaniti)
-
Ummm...didn't you guys just suggest the inverse of what the original poster was asking for?
It's been awhile since I've coded and quite likely my brain's fuzzy, but I thought he was asking for a function with a numerical argument that would return a string, not a function with a string argument that would return a number.
:-)
EDIT: oh sure! Clear up the above post that came out right when I ws posting to make me look like the guy who can't read the whole thread! :-P
Am I reading that right? Does the above code support up to base 36?
-
:pint: :pint:
-
sprintf(outstr,"%ld",l);
sprintf(outstr,"%d",i);
-
@Wain
Am I reading that right? Does the above code support up to base 36?
It sure does. Apparently there are different kinds of implementations of the function (it's non-std after all).
This one looks quite complete. Check the discussion in the google link of my previous post.
[oops, the link was broken, fixed now..]
-
@vic20owner
Yeah well, but that only covers base == 10 case.