Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Jose on September 09, 2005, 07:54:20 PM

Title: itoa && ltoa in VBCC
Post 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:
Title: Re: itoa && ltoa in VBCC
Post by: Thomas on September 09, 2005, 09:41:40 PM
You should go to http://sun.hasenbraten.de/vbcc/ and open a bug report.

Nevertheless I would use one of these:

Code: [Select]

long atol (char *s)

{
long i = 0;

sscanf (s,"%ld",&i);

return (i);
}



Code: [Select]

long atol (char *s)

{
long i = 0;

while (*s >= '0' && *s <= '9')
{
i = (i * 10) + (*s - '0');
s ++;
}

return (i);
}

Title: Re: itoa && ltoa in VBCC
Post by: Piru on September 09, 2005, 10:11:41 PM
Quote
Code: [Select]
long atol (char *s)

{
long i = 0;

while (*s >= '0' && *s <= '9')
        {
        i = (i * 10) + (*s - '0');
        s ++;
        }

return (i);
}

And a working version:
Code: [Select]

#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;
}

Title: Re: itoa && ltoa in VBCC
Post by: Piru on September 09, 2005, 10:25:06 PM
Quote
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).

Code: [Select]
// 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 = &quot;0123456789abcdefghijklmnopqrstuvwxyz&quot;[i % base];
        i /= base;
    } while (i);
    if (sign) *--s = '-';
    return strcpy(buf, s);
}


(From the abovementioned post by John Passaniti)
Title: Re: itoa && ltoa in VBCC
Post by: Wain on September 09, 2005, 10:27:47 PM
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?
Title: Re: itoa && ltoa in VBCC
Post by: Jose on September 10, 2005, 11:05:55 AM
:pint: :pint:
Title: Re: itoa && ltoa in VBCC
Post by: vic20owner on September 25, 2005, 02:35:11 AM

sprintf(outstr,"%ld",l);
sprintf(outstr,"%d",i);

Title: Re: itoa && ltoa in VBCC
Post by: Piru on September 25, 2005, 03:17:22 AM
@Wain
Quote
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..]
Title: Re: itoa && ltoa in VBCC
Post by: Piru on September 25, 2005, 03:20:10 AM
@vic20owner

Yeah well, but that only covers base == 10 case.