Welcome, Guest. Please login or register.

Author Topic: itoa && ltoa in VBCC  (Read 2268 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
itoa && ltoa in VBCC
« 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:
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Thomas

Re: itoa && ltoa in VBCC
« Reply #1 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);
}


Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: itoa && ltoa in VBCC
« Reply #2 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;
}

 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: itoa && ltoa in VBCC
« Reply #3 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.

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)
 

Offline Wain

  • Hero Member
  • *****
  • Join Date: Sep 2002
  • Posts: 745
    • Show only replies by Wain
Re: itoa && ltoa in VBCC
« Reply #4 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?
Professional Expatriate
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: itoa && ltoa in VBCC
« Reply #5 on: September 10, 2005, 11:05:55 AM »
:pint: :pint:
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline vic20owner

  • Sr. Member
  • ****
  • Join Date: Jun 2004
  • Posts: 400
    • Show only replies by vic20owner
Re: itoa && ltoa in VBCC
« Reply #6 on: September 25, 2005, 02:35:11 AM »

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

Amiga 1200 030/50mhz 64MB Fast Ram 20GB HD
DTCV, S-Video hack, 1084S-D1, PCMCIA Wireless
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: itoa && ltoa in VBCC
« Reply #7 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..]
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: itoa && ltoa in VBCC
« Reply #8 on: September 25, 2005, 03:20:10 AM »
@vic20owner

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